| delorie.com/archives/browse.cgi | search | 
| From: | Mark Phillips <umphill5 AT cs DOT umanitoba DOT ca> | 
| Newsgroups: | comp.os.msdos.djgpp | 
| Subject: | Re: Random Numbers | 
| Date: | Fri, 16 Jul 1999 09:44:59 -0500 | 
| Organization: | The University of Manitoba | 
| Lines: | 23 | 
| Message-ID: | <Pine.SUN.3.96.990716093758.6668A-100000@silver.cs.umanitoba.ca> | 
| NNTP-Posting-Host: | silver.cs.umanitoba.ca | 
| Mime-Version: | 1.0 | 
| X-Trace: | canopus.cc.umanitoba.ca 932136306 9192 130.179.24.6 (16 Jul 1999 14:45:06 GMT) | 
| X-Complaints-To: | Postmaster AT cc DOT umanitoba DOT ca | 
| NNTP-Posting-Date: | 16 Jul 1999 14:45:06 GMT | 
| To: | djgpp AT delorie DOT com | 
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp | 
| Reply-To: | djgpp AT delorie DOT com | 
Well I've seen people get a random number like this: int random_number1to100 = ( rand()%100 ) + 1; But I've also heard that that isn't so good because on some random number generators, the low order bits aren't that random. You could also do: double random_number0to1 = double( rand()/MAX_RANDOM ); That should give you a float from 0 to 1 which you can use your BASIC methods on to convert to any range of numbers. I think the maximum value a random number can take (MAX_RANDOM here) is the same as MAX_INT. Anybody?? If you want to avoid floating point math you could try: int random_number1to100 = 100 * rand() / MAX_RANDOM + 1; but that might cause overflow. And you can't solve that by making it rand() / MAX_RANDOM * 100 + 1 because that will always give you 1. Mark
| webmaster | delorie software privacy | 
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |