avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] rand()


From: Oliver Kasten
Subject: Re: [avr-gcc-list] rand()
Date: Sat, 27 Jul 2002 11:34:06 +0200

hi there,

below is the code taken from the well-written intro to random number
generators:

"Random number Generators: Good Ones Are Hard To Find", Park, Stephen
K., and Miller, Keith W., Communications of the ACM, October, 1988. 

the problem is finding a good seed. since i have bluetooth attached to
the avr (mega128), i use (the lower part of) the bluetooth device
address. in the code below "s32" is a signed integer worth of 32 bit. in
the gnu avr-gcc that would be a "long".

best,
olli*

-- snip --

void srand( s32 new_seed  );

Initialize the starting point in the sequence of pseudo random numbers
(known as seed). 



s32 rand (); 

Returns a pseudo random number.

The algorithm returns a pseudo random number between 0 and 2147483647
(2^31 - 1), inclusive. The generator is pseudo random, that is, the
numbers generated are by no means random. Starting from a seed, always
the same sequence of numbers is generated. srand() can be used to
initialize the seed. 

The generator has a full period (of 2147483647), that is, all 2147483647
numbers are generated before the sequence repeats. 



// ------------------------------------------------------------
// globals
// ------------------------------------------------------------

// This is required to be 32 bits long
static s32 seed = 1;

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
void srand( s32 new_seed ) {
  seed = new_seed;
}


// ------------------------------------------------------------
// 
// ------------------------------------------------------------
/* Random number between 0 and  2147483647 (2^31 - 1)  inclusive */

s32 rand() {

  // do not change these numbers unless you know what you're doing!

  s32 a =      16807; // multiplier
  s32 m = 2147483647; // mantissa
  s32 q =     127773; // q = m div a
  s32 r =       2836; // r = m mod a

  s32 test = a * (seed % q) - r * (seed /q);

  if( test > 0)
    seed = test;
  else
    seed = test + m;

  return seed;
}


Torsten Hahn wrote:
> 
> Hi,
> 
> has somebody still implemented an random number generator on the avr ? May be,
> it would be nice such a function in the avr-libc. Random number routines are
> not the problem i think but how to generate a "seed". On a pc you can use the
> time() function. Has somebody an idea how to do it on avr ?
> 
> cu,
> Torsten.
> --
> Torsten Hahn
> TU Bergakademie Freiberg - Institut f├╝r Experimentelle Physik
> Silbermannstra├če 1, 09596 Freiberg
> mail: address@hidden
> phone: +49 3731 392670
> avr-gcc-list at http://avr1.org


-- 
Oliver Kasten                                phone: +41/ 1/ 63-2 06 63
ETH-Zurich                                     fax: +41/ 1/ 63-2 16 59
Haldeneggsteig 4, IFW D48.1           email: address@hidden
CH-8092 Zuerich, Switzerland           http://www.inf.ethz.ch/~kasten/
avr-gcc-list at http://avr1.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]