I use standard C random number generation logic in my application.
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
Last edited by Yogesh Sawant; 08-17-2009 at 09:34 AM..
Reason: added code tags
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.
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)
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)
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)
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)
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)
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)
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)
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)