![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Does Solaris have a random number generator? | one_ring99 | SUN Solaris | 3 | 04-10-2008 01:39 AM |
| generate random number in perl | zx1106 | Shell Programming and Scripting | 2 | 03-17-2008 09:13 PM |
| Random number generation in ksh | mervin2006 | UNIX for Dummies Questions & Answers | 2 | 04-26-2007 11:02 PM |
| Random number generation | tej.buch | High Level Programming | 1 | 02-13-2006 07:07 AM |
| How to generate a random number? | MacMonster | High Level Programming | 2 | 10-15-2001 09:35 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
random number logic -- issue
I use standard C random number generation logic in my application.
------------------------------------------------- long nCounter; long lRndNo; char rand[11]; srand48(nCounter); lRndNo = lrand48(); sprintf(rand,"%010.10d",lRndNo); -------------------------------------------------- However we always find that the rand generated starts with '0','1' or '2'. I could not logically prove if is guaranteed to start with these. We are designing a new program with the assumption that it will not start with '5'. Can someone please explain how this logic (linear conguential) works to start with '0','1','2'? I will highly appreciate. Thanks Asutosh |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Please don't post the same question twice. I deleted the duplicate.
The largest number in a 32 bit signed integer is 2,147,483,647. So if a 32 bit signed integer has more than 9 significant digits, it will have exactly 10 and the most significant digit will be a 1 or a 2. There is no way to store a larger number in 32 bits. So you are building in an assumption of 32 bit arithmetic. That sounds dangerous today. There are quite a few 64 bit machines around. Or the other hand, you are calling srand48(). What makes srand48() cool is that it uses 48 bit aritmetic internally and returns a 32 bit quantity. An assumpion of 32 bit arithmetic is built heavily into srand48(), even up to its name. So maybe you can get away with it. |
||||
| Google The UNIX and Linux Forums |