comp.lang.c
[Top] [All Lists]

Re: simple PRNG?

Subject: Re: simple PRNG?
From: pete
Date: Fri, 28 Mar 2008 15:35:25 -0500
Newsgroups: comp.lang.c

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

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy