Re: simple PRNG?



copx wrote:

Can anyone point me to a simple,
fast RRNG function to generate random ints
within a specified range?

I use 2 different 32 bit prng's.
1 The fast one
2 The good one

Both of them are used here:
http://www.mindspring.com/~pfilandr/C/q_sort/q_sort.c

This is the fast one:
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069 + 362437 & 0XFFFFFFFFLU)
It is used in the qrsort function.

This is the good one:
#define SEED_RAND 123456789
#define SEED_0 0
#define SEED_1 SEED_RAND
#define SEED_2 362436069
#define SEED_3 521288629
#define SEED_4 88675123
#define SEED_5 886756453
#define SEEDS {SEED_0, SEED_1, SEED_2, \
SEED_3, SEED_4, SEED_5}
#define LUS_RAND(S) \
(S[0] = S[1] ^ S[1] >> 7, \
S[1] = S[2], \
S[2] = S[3], \
S[3] = S[4], \
S[4] = S[5], \
S[5] = (S[5] ^ S[5] << 6 \
^ S[0] ^ S[0] << 13) & 0xffffffff, \
S[5] * (S[2] + S[2] + 1) & 0xffffffff)
It is used in the main function.


There's also the example described in the standard:
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;
}


--
pete
.



Relevant Pages

  • Re: simple PRNG?
    ... within a specified range? ... Probably the best bit-size independent algorithm is ... It is very simple if you can spare the space for the state (min is ... N=64 with each R being a 64-bit int. ...
    (comp.lang.c)
  • Re: A little help please
    ... >>The function should set each byte in the specified range to the value. ... Yes the question itself also confuses me somewhat, ... Define an array of int. ...
    (alt.comp.lang.learn.c-cpp)
  • [PATCH 17/24] lmb: Add lmb_memory_size()
    ... It will return memory size in specified range according to lmb.memory.region ... u64 lmb_hole_size; ... int i, count; ...
    (Linux-Kernel)
  • [PATCH 17/31] lmb: Add lmb_memory_size()
    ... It will return memory size in specified range according to lmb.memory.region ... u64 lmb_hole_size; ... int i, count; ...
    (Linux-Kernel)
  • Re: a gift for the mortensens
    ... int main ... None of the three casts in your program are necessary, ... If plain char is signed, the conversion of i from int to char (which ... produce numbers within a specified range, ...
    (comp.lang.c)