The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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 08: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 06:07 AM
How to generate a random number? MacMonster High Level Programming 2 10-15-2001 09:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2003
Registered User
 

Join Date: Mar 2001
Location: Calcutta, India
Posts: 46
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-09-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0