473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pseudorandom algorithms

I need a pseudorandom algorithm that produces same results with same
seed across different platforms. I'm not sure, but I think that
standard rand() does produce different set of numbers on different
platforms. I would be using only four distinct values,
i.e. (rand() % 4).

This algorithm would be used to generate a bitmap when a game starts,
and if randomness is platform-variable, the game will look different
on different platforms and this is not what I want.
--
C faq: http://www.eskimo.com/~scs/C-faq/top.html
Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
Coding standards: http://www.psgd.org/paul/docs/cstyle/cstyle.htm
Dec 21 '05 #1
4 1534
Tatu Portin wrote:
I need a pseudorandom algorithm that produces same results with same
seed across different platforms. I'm not sure, but I think that
standard rand() does produce different set of numbers on different
platforms. I would be using only four distinct values,
i.e. (rand() % 4).

This algorithm would be used to generate a bitmap when a game starts,
and if randomness is platform-variable, the game will look different
on different platforms and this is not what I want.


The standard does not prescribe much w.r.t. rand().
So, if you need a pseudorandom number generator which behaves
identically for identical seeds througout different platforms,
write one yourself. Take into account that integer type widths
may vary.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 21 '05 #2
Tatu Portin <ax****@mbnet.fi> writes:
I need a pseudorandom algorithm that produces same results with same
seed across different platforms. I'm not sure, but I think that
standard rand() does produce different set of numbers on different
platforms. I would be using only four distinct values,
i.e. (rand() % 4).

This algorithm would be used to generate a bitmap when a game starts,
and if randomness is platform-variable, the game will look different
on different platforms and this is not what I want.


Right, there's no requirement for all C implementations to use the
same algorithm, but there is a sample algorithm in the standard.
C99 7.20.2.2:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
next = seed;
}

If you change the names of the functions (so they don't conflict with
the standard functions), I *think* the above code will give you the
same sequence for the same seed on all platforms. (Don't take my word
for that; try it on all platforms you're interested in.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 21 '05 #3
Tatu Portin <ax****@mbnet.fi> writes:
I need a pseudorandom algorithm that produces same results with same
seed across different platforms. I'm not sure, but I think that
standard rand() does produce different set of numbers on different
platforms. I would be using only four distinct values,
i.e. (rand() % 4).

This algorithm would be used to generate a bitmap when a game starts,
and if randomness is platform-variable, the game will look different
on different platforms and this is not what I want.


/***** This file is prand32.h *****/
#ifndef H_PRAND32_H
#define H_PRAND32_H

/************************************************** ************************/
/* A portable, platform independent, pseudo-random number generator. */
/* */
/* random_32() yields a 32-bit, uniformly distributed, "random number". */
/* */
/* Use seed_random_32() to supply a 32-bit seed value for random_32(). */
/************************************************** ************************/
#define random_32() (*--random_32_p ? *random_32_p : cycle_random_32())

extern void seed_random_32( unsigned long );
extern unsigned long (random_32)( void );
/*************************/
/* for internal use only */
/*************************/
extern unsigned long cycle_random_32( void );
extern const unsigned long *random_32_p;
#endif /* H_PRAND_H */
/***** End of prand32.h *****/


/***** This file is prand32.c *****/
#include "prand32.h"
/************************************************** *********/
/* The method employed is based on a lagged fibonacci PRNG */
/* given in Knuth's "Graph Base" book. */
/************************************************** *********/

#define VALUES_N (55u)
#define VALUES_D (24u)
/* Other reasonable values: 33,20 17,11 9,5 */
#define VALUES_INDEX_LIMIT (VALUES_N + 1)

static unsigned long values[VALUES_INDEX_LIMIT];
const unsigned long *random_32_p = values + VALUES_INDEX_LIMIT;
static const unsigned long mask32 = 0xffffffffUL;
void
seed_random_32( unsigned long seed_value ){
unsigned i;

values[1] = seed_value & mask32;
for( i = 2; i < VALUES_INDEX_LIMIT; i++ ) values[i] = 0;

for( i = 0; i < VALUES_INDEX_LIMIT * 2; i++ ){
random_32_p = &values[0];
(void) cycle_random_32();
}
}
unsigned long
(random_32)( void ){
return random_32();
}
unsigned long
cycle_random_32(){
unsigned i;

if( random_32_p == &values[0] ){
for( i = 1; i + VALUES_D < VALUES_INDEX_LIMIT; i++ ){
values[i] -= values[ i+VALUES_D ];
values[i] &= mask32;
}
for( i = i; i < VALUES_INDEX_LIMIT; i++ ){
values[i] -= values[ i - (VALUES_N - VALUES_D) ];
values[i] &= mask32;
}

for( i = 1; i < VALUES_INDEX_LIMIT; i++ ){
values[i] ^= (mask32 ^ values[i-1]) >> 1;
}

random_32_p = &values[VALUES_INDEX_LIMIT - 1];

} else if( random_32_p == &values[VALUES_INDEX_LIMIT - 1] ){
seed_random_32( 0 );
}

return *random_32_p;
}

/***** End of prand32.c *****/
Query to comp.lang.c readers: What platform dependencies
did I miss?
Dec 29 '05 #4

Keith Thompson schreef:
Tatu Portin <ax****@mbnet.fi> writes:
I need a pseudorandom algorithm that produces same results with same
seed across different platforms. I'm not sure, but I think that
standard rand() does produce different set of numbers on different
platforms. I would be using only four distinct values,
i.e. (rand() % 4).

This algorithm would be used to generate a bitmap when a game starts,
and if randomness is platform-variable, the game will look different
on different platforms and this is not what I want.


Right, there's no requirement for all C implementations to use the
same algorithm, but there is a sample algorithm in the standard.
C99 7.20.2.2:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
next = seed;
}

If you change the names of the functions (so they don't conflict with
the standard functions), I *think* the above code will give you the
same sequence for the same seed on all platforms. (Don't take my word
for that; try it on all platforms you're interested in.)


Assuming all have a 32-bit int (or bigger) and there aren't any bugs in
basic arithmatic instructions, i don't really see how the results
_could_ differ. After all, it's fairly basic math and I totally fail to
spot any possible implementation or platform dependencies.

However, as Dr. Knuth wrote: "be wary of bugs in the above code. I've
only proven it correct, not actually tried it."

Dec 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Oplec | last post by:
Hello, I am learning the C++ programming language as a hobby. I have found some useful links such as the C++ FAQ Lite, Flipcode, Gamedev, and Gamasutra that are good for information/help. However,...
6
by: Fraser Ross | last post by:
Algorithms cannot be used with input stream iterators? Is copying the range to a temporary container before using the algorithm the usual thing to do? Fraser.
7
by: Anders Borum | last post by:
Hello! I'm starting to dive into algorithms in general (sorting, trees etc.) and am currently reading a book from Robert Sedgewick called "Algorithms in C++, 3rd. Edition" and would like other...
26
by: vlsidesign | last post by:
I am a newbie and going through "The C programming language" by Kernighan & Richie on my own time (I'm not a programmer but I want to learn because it can save me time in my normal job, and it is...
3
by: arnuld | last post by:
i am looking for "algorithms in C++" book. Knuth is FULL of Mathematics, not my kind of author. i checked ACCU and got these (listing only those that are available in my country: 1. Algorithms...
17
by: Happy Man | last post by:
Truth Seeker http://www.thisistruth.org/truth.php?f=TruthSeeker No one is compelled to accept the truth, but it is certainly a shame upon the human intellect when a man is not even...
2
by: =?Utf-8?B?Q3JtTmV3Ymll?= | last post by:
Hi, 1) I want to hone my problem solving skills and be good at logic. How do I achieve this? 2) Where can I find c# puzzles or c# algorithms. 3) How do I print the values of a N X N matrix...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
12
by: subramanian100in | last post by:
Below is my understanding about count algorithms. Return type of count and count_if algorithms is iterator_traits<InputIterator>::difference_type. If the container contains more than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.