Sponsored Content
Full Discussion: random number logic -- issue
Top Forums Programming random number logic -- issue Post 35864 by Perderabo on Friday 9th of May 2003 11:07:32 AM
Old 05-09-2003
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.
 

10 More Discussions You Might Find Interesting

1. Programming

How to generate a random number?

How to generate a random integer with specific range(for example, from 1 to 1000)? Also, how to convert a floating point number into a integer? (2 Replies)
Discussion started by: MacMonster
2 Replies

2. Programming

Random number generation

Hi...How can I generate random numbers between a given range ...preferably between 1 and 100...in UNIX C programming...? I tried out functions like rand(),drand48() etc but didnt get satisfactory results... Thanks a lot in advance.......... (1 Reply)
Discussion started by: tej.buch
1 Replies

3. Shell Programming and Scripting

Regarding Random Number Genration

HI please help me in solving this issue. I have a shell script which consists of ten statemnts ( i mean 10 executable statments)...........and if i run that script the 10 statmenst will execute continously ............ But now my probelm is i have to write another script --> to... (0 Replies)
Discussion started by: lalitka
0 Replies

4. Shell Programming and Scripting

ksh: random number between 1-10

How do I create a random number between 1 and 10 in kornshell? (2 Replies)
Discussion started by: dangral
2 Replies

5. Programming

C Help; generating a random number.

Im new to C, and Im having a hard time getting a random number. In bash, I would do something similar to the following to get a random number; #!/bin/bash seed1=$RANDOM seed2=$RANDOM seed3=$RANDOM SEED=`expr $seed1 * $seed2 / $seed3` echo ${SEED%.*} Now, in online examples... (4 Replies)
Discussion started by: trey85stang
4 Replies

6. Programming

Random number is not reused

I need to generate and reuse a 5 digit random number every time my program is executed. But the following generates random numbers every time the function is called. #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <time.h> ... (12 Replies)
Discussion started by: limmer
12 Replies

7. Programming

Generate random number

I saw this formula to generate random number between two specified values in shell script.the following. $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min)) Give a example in book. Generate random number between 6 and 30.like this. $(((RANDOM%30/3+1)*3)) But I have a... (1 Reply)
Discussion started by: luoluo
1 Replies

8. Shell Programming and Scripting

Random pieces of number

Hello folks, i have number for example 10 and i want to divide into 4 random pieces that may be (6+2+1+1). How can i do this via script i have random number 234951 and i want to divide into 31 pieces. (6 Replies)
Discussion started by: learnbash
6 Replies

9. Shell Programming and Scripting

Create random number

Hi, I'm trying to create a script that will print random numbers with length of three. Below is the expected out. 928-377-899 942-458-310 951-948-511 962-681-415 995-161-708 997-997-209 thanks (4 Replies)
Discussion started by: reignangel2003
4 Replies

10. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 Replies
DRAND48(3)						     Linux Programmer's Manual							DRAND48(3)

NAME
drand48, erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48, lcong48 - generate uniformly distributed pseudo-random numbers SYNOPSIS
#include <stdlib.h> double drand48(void); double erand48(unsigned short xsubi[3]); long int lrand48(void); long int nrand48(unsigned short xsubi[3]); long int mrand48(void); long int jrand48(unsigned short xsubi[3]); void srand48(long int seedval); unsigned short *seed48(unsigned short seed16v[3]); void lcong48(unsigned short param[7]); DESCRIPTION
These functions generate pseudo-random numbers using the linear congruential algorithm and 48-bit integer arithmetic. The drand48() and erand48() functions return non-negative double-precision floating-point values uniformly distributed between [0.0, 1.0). The lrand48() and nrand48() functions return non-negative long integers uniformly distributed between 0 and 2^31. The mrand48() and jrand48() functions return signed long integers uniformly distributed between -2^31 and 2^31. The srand48(), seed48() and lcong48() functions are initialization functions, one of which should be called before using drand48(), lrand48() or mrand48(). The functions erand48(), nrand48() and jrand48() do not require an initialization function to be called first. All the functions work by generating a sequence of 48-bit integers, Xi, according to the linear congruential formula: Xn+1 = (aXn + c) mod m, where n >= 0 The parameter m = 2^48, hence 48-bit integer arithmetic is performed. Unless lcong48() is called, a and c are given by: a = 0x5DEECE66D c = 0xB The value returned by any of the functions drand48(), erand48(), lrand48(), nrand48(), mrand48() or jrand48() is computed by first generat- ing the next 48-bit Xi in the sequence. Then the appropriate number of bits, according to the type of data item to be returned, is copied from the high-order bits of Xi and transformed into the returned value. The functions drand48(), lrand48() and mrand48() store the last 48-bit Xi generated in an internal buffer. The functions erand48(), nrand48() and jrand48() require the calling program to provide storage for the successive Xi values in the array argument xsubi. The func- tions are initialized by placing the initial value of Xi into the array before calling the function for the first time. The initializer function srand48() sets the high order 32-bits of Xi to the argument seedval. The low order 16-bits are set to the arbi- trary value 0x330E. The initializer function seed48() sets the value of Xi to the 48-bit value specified in the array argument seed16v. The previous value of Xi is copied into an internal buffer and a pointer to this buffer is returned by seed48(). The initialization function lcong48() allows the user to specify initial values for Xi, a and c. Array argument elements param[0-2] spec- ify Xi, param[3-5] specify a, and param[6] specifies c. After lcong48() has been called, a subsequent call to either srand48() or seed48() will restore the standard values of a and c. CONFORMING TO
SVID 3 NOTES
These functions are declared obsolete by SVID 3, which states that rand(3) should be used instead. SEE ALSO
rand(3), random(3) 1993-07-02 DRAND48(3)
All times are GMT -4. The time now is 07:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy