ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
srand() is used to seed the random number generator, not get random numbers. Are you asking for a list of numbers that each have 8 digits? Something like 98239478?
The first one should work with any C compiler. The second one might be gcc-specific.
time(0) doesn't need to be the seed for the random number generator. You can also use things like getpid() for the seed, but the key is to take the seed from a varying source so you get a different set of random numbers each time.
Prototype: void srand(unsigned int seed);
Obviously the seed has to change every time the program is run. One way to do this is to feed it the O/P from time
time(0); // get the calender time, Number of seconds since 1st Jan 1970.
after a call to srand, rand() will generate a random number between 0 and 'RAND_MAX', which has a more random value!
without using srand and time(0) as its parameter (shich differs from time to time), each time you run your program, the sequence if random numbers returned by rand() is the same.
Because if the first digit is a 0, if you convert the string to a number and then display it, you'll only get 7 digits (it won't show something like 07423489, it just shows 7423489). So to ensure that you get an 8 digit number it makes the first digit be 1 through 9 instead of 0 through 9 like the rest of them.
The rand() function returns a number from 0 up to RAND_MAX. The % operator divides by the number on the left by the number on the right and then returns the remainder. So basically this makes rand() return a number between 0 and the number on the right side of the % instead.
I was wondering how exactly the following statement works:
num[0] = '1'+rand()%9;
I was puzzled how adding a number (rand()%9) to a character ('1') works, but I eventually figured it out, remembering an example from "The C Programming Language." Here's my explanation, in case someone has the same question:
A character is actually just a small integer. For instance, according to http://www.asciitable.com/, a '1' is just 49. And it so happens that the ascii code defines the digits '0' through '9' consecutively ('0' is 48, '1' is 49, '2' is 50, etc.), so an expression like '1' + 2 (2 is one possible value of 'rand()%9', for example), would give 51, which corresponds to a '3' being stored in num[0] -- the first element of the character array.
There are two solutions i can remember to random but unique numbers problem. First one is using an extra array of numbers and copying all the numbers you generate to this array and checking all newly generated numbers with this array; if exist create a new one again steps. But this consumes too much memory --especially with 8 digits numbers which requires bigger type than int- and CPU time --assume that you have to create too much unique number- The main idea here is creating a sequential array of 8-digits numbers (like 10000000,10000001,10000002,.. or 10000001,10000004,10000009) and swapping the numbers randomly (select two numbers randomly and swap them). Then you will get a 8-digit random number array.. Hope this helps.
Originally posted by suchi_s when i run this prog for 40000 numbers some of the numbers are same..
(means repetitive occurance) why?
how to overcome
Quote:
Originally posted by suchi_s if im run the progranm again after 2 mins for next 40000 numbers some of the numbers are same which were generated previously
Well, of course this can happen. Random is random, and if you generate 40000 out of 90000000 possible numbers, there's a reasonable chance there will be duplicates.
However, then rand() function from glibc is not bad, but it may be not strong enough for your tests.
Here's a program that uses the random generator from the Linux-kernel (if it is configured in your kernel. Probably it is.). I suppose this is a better random generator than the one from glibc.
It also generates 8-digit numbers, but it includes numbers starting with one or more zeroes (e.g. 00234978 is possible). (If you don't want this, it can be easily fixed the same way as the example's given earlier in the thread).
Code:
#include <stdio.h>
int main()
{
unsigned int rnd;
FILE *fp;
if ((fp = fopen("/dev/urandom", "r")) == NULL) {
perror("");
return 1;
}
if (fread(&rnd, 1, sizeof(rnd), fp) != sizeof(rnd)) {
fprintf(stderr, "Error reading random number from device file\n");
return 1;
}
rnd %= 100000000; /* A '1' with 8 zeroes (for 8 digits) */
printf("%.8u\n", rnd); /* print at least 8 digits, padding with 0's if needed */
return 0;
}
I ran a few tests, but it also has duplicates in the results. This is normal, throw a dice 40000 times, and I'm sure you will have duplicates
To test for duplicates, I used the command below. It prints only numbers that ocurred more than once. (the compiled random program is called "randdigits" here)
Code:
for i in `seq 1 40000` ; do ./randdigits ; done | sort | uniq -d
If you really don't want duplicates in the result, stick with barisdemiray's suggestion, and maybe combine it with my program for (arguably ?) better randomization.
From what I understand, /dev/urandom is truely random as opposed to the rand() function which is only pseudo random. rand() generates "random" numbers by numeric computation. /dev/urandom is built from harddrive accesses and other events that, in combination with each other, produce true randomness. The key is that /dev/urandom is built using random input like user key presses and such.
But just because it's truely random doesn't mean you'll never get duplicate results. Here's a program that will give you 40,000 unique 8-digit random numbers.
When the outer loop finishes, numlist will contain your list of unique 8-digit numbers. If you add a loop that prints the list of numbers and run it like I did below, you'll see that it works.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.