| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Random Seed |
Sean Price wrote in a message to Darin McBride: DM> What are you seeding? Try using time(NULL), for an example. SP> I just want to seed the standard rand() function. Be careful: srand() takes an unsigned int, while time() returns a time_t. These are different sizes on many compilers. To handle this portably, do something like this: #include #include // for UINT_MAX #include srand((unsigned int)(time(NULL) % UINT_MAX)); Note that this is NOT necessarily guaranteed to give you a uniform distribution of inputs to srand(), although it will in any practical implementation where time() returns a 32-bit number and srand() takes an int which is either a 16-bit or 32-bit number. If you need to guarantee uniform distribution of inputs to srand(), the proper solution is to apply a hash (such as the ELF hash I discussed in a prior message to you) and a prime modulus less than INT_MAX before feeding the seed to srand(). Note especially that the random number generators provided with most compilers are awful. Borland's and Watcom's are particularly bad, since even their 32-bit OS/2 compilers only return 16 bits of significance from rand(). ANSI C guarantees that RAND_MAX is at least 32767, and Watcom gives you the bare minimum. Just because rand() returns a 32-bit int does not necessarily imply that the most significant 16 bits are ever non-zero. If you have any kind of serious need for a random number generator, you should use one of the widely accepted ones from the published literature. As I mentioned earlier, Bryan Flamig's "Practical Algorithms in C++" has a very thorough, readable, and up to date treatment of this subject. -- Mike ---* Origin: N1BEE BBS +1 401 944 8498 V.34/V.FC/V.32bis/HST16.8 (1:323/107) SEEN-BY: 50/99 270/101 620/243 711/401 409 410 413 430 808 809 934 955 SEEN-BY: 712/407 515 517 628 713/888 800/1 @PATH: 323/107 396/1 270/101 712/515 711/808 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.