[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-gcc-list] RandLCG()
From: |
Torsten Hahn |
Subject: |
[avr-gcc-list] RandLCG() |
Date: |
Thu, 11 Jul 2002 13:34:07 +0200 |
Hi,
after a bit reading in Donald E. Knuth's Books i have implemented a fast
pseudo random number generator in assembler for the avr-devices. A linear
congruence generator is used to produce 16 Bit pseudo random numbers. The
function needs only 22 cycles + return. If you need less than the 16 Bit, you
should use the MSB of the result.
For my needs, the generator works very well.
May be, somebody will find it usefull.
Usage: (generate an array with 10 random numbers)
extern uint16_t RandLCG(uint16_t seed);
void generateNumbers(void)
{
uint16_t seed = 0; // set the start seed
uint16_t rnd_numbers[10];
uint8_t i;
for (i=0;i<10;i++)
{
seed = RandLCG(seed);
rnd_numbers[i] = seed;
}
// do something with the numbers ...
} // generateNumbers
(May be, it would be nice to add something like this to the avr-libc ;))
cu,
Torsten.
--
Torsten Hahn
TU Bergakademie Freiberg - Institut für Experimentelle Physik
Silbermannstraße 1, 09596 Freiberg
mail: address@hidden
phone: +49 3731 392670
rand.S
Description: Text Data
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [avr-gcc-list] RandLCG(),
Torsten Hahn <=