| www.delorie.com/djgpp/bugs/show.cgi | search |
rand() has a period of less than 30 million, even though it uses 64 bits of state. Very simple, fast generators that just do a multiply and add are available that have the maximum possible period and provably good characteristics. I'll provide one.
How did you find this short period : I tried it on my machine and could not find any short period (it went over 1 billion 300 millions iterations with no repeat...). However, if it was the case, removing the next =(next>>27)^(next<<15); instruction should suffice, you hence get a plain linear congruential generator, and as 2**64 and 1103515245 are prime in pairs, period will be 2**64... Finally, if you want to improve such a generator you may change the congruent values : next=16807*next (mod 2147483647) has been used with much success for some time.
Remove the line : next=(next>>15)^(next<<27); from function rand(). You get a normal long period random nr generator You can also change all the (long long) types to (unsigned long), the (unsigned long) generator has a perios over 4 billion, which is more than enough. And I think rand() will be more rapid, and more alike to other ANSI implementations.
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2010 by DJ Delorie | Updated Jul 2010 |