RomanRega
asked on
Best way to generate random numbers in C
As the subject.
What do you mean by "best way" ?? The fastest? The most mathematically pure algorithm? Truly random, or pseudo-random? How many do you need? How soon do you need them? What tests for randomness are you going to use? All important questions.
If you are looking for the easiest way, look into help/man pages for srand() and rand()
If you are looking for some fair shuffling with no/little repetitions, you can draw some help from these links
https://www.experts-exchange.com/questions/20688715/generating-numbers-faster.html
https://www.experts-exchange.com/questions/10216999/Card-Shuffling-algorithm.html
Cheers!
sunnycoder
If you are looking for some fair shuffling with no/little repetitions, you can draw some help from these links
https://www.experts-exchange.com/questions/20688715/generating-numbers-faster.html
https://www.experts-exchange.com/questions/10216999/Card-Shuffling-algorithm.html
Cheers!
sunnycoder
If you want to go deep into random numbers, here are some interesting pages:
http://www.taygeta.com/random.html
http://random.mat.sbg.ac.at/
http://www.mandala.co.uk/links/random/
http://www.math.utah.edu/~alfeld/Random/Random.html
http://www.taygeta.com/random.html
http://random.mat.sbg.ac.at/
http://www.mandala.co.uk/links/random/
http://www.math.utah.edu/~alfeld/Random/Random.html
remember... unless you have a quantum computer or a nice laser, all random numbers are replicatable. by adding things like the nth digit of the CPU temperature, the ambiant temperature, or the measurable quantum variance of your mother boards FSB; you can get another random number, but it is no better than a psudeo random number generated by the current time cleverly manipulated by cosine curves and other combinatorics.
I find that cat'ing random for a random amount of time, then taking a range from a random poition is the easiest way to do it. srand can be harder to use... it can actually return nothing if the random numbers it generates is not up to snuff. funny, but hey.
I find that cat'ing random for a random amount of time, then taking a range from a random poition is the easiest way to do it. srand can be harder to use... it can actually return nothing if the random numbers it generates is not up to snuff. funny, but hey.
The truth is there's no such thing as "pure randomness" as far as generating random numbers are concerned. You can "fake" it, but it's ultimately something of a hack. I've found that for most things, though, it never really matters, so I stick with srand and rand.
srand (time (NULL));
And then rand () % SOME_MAXIMUM_NUMBER
will generate a number between 0 and SOME_MAXIMUM_NUMBER
Don't expect PURE randomness, though; that sort of thing just won't happen with rand ().
srand (time (NULL));
And then rand () % SOME_MAXIMUM_NUMBER
will generate a number between 0 and SOME_MAXIMUM_NUMBER
Don't expect PURE randomness, though; that sort of thing just won't happen with rand ().
This is pretty random:
rand()*(int)time();
The time function returns the number of seconds after 1-1-1970, so you have to start two programs at exactly the same second to get the same number twice.
ps.
include <time.h>
Stuart
rand()*(int)time();
The time function returns the number of seconds after 1-1-1970, so you have to start two programs at exactly the same second to get the same number twice.
ps.
include <time.h>
Stuart
to generate numbers from 1 to 10 the easiest way is
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
srand(time(0));
int rnum;
for (int i=0; i<10; i++)
{
rnum=rand()%11;
printf("%d",rnum);
}//for
getch();
}
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
srand(time(0));
int rnum;
for (int i=0; i<10; i++)
{
rnum=rand()%11;
printf("%d",rnum);
}//for
getch();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There are a number of sites which cover the subject - just search for Random Number Generation Theory or some variant on those words. Most will have code samples too.