Link to home
Start Free TrialLog in
Avatar of RomanRega
RomanRega

asked on

Best way to generate random numbers in C

As the subject.
Avatar of Barthax
Barthax
Flag of United Kingdom of Great Britain and Northern Ireland image

The problem will all algorithms that use random numbers is they are predictable - given a starting point the algorithm will generate the same number sequence time and again.  So the idea is to get the random number generator to perform it's calculations frequently when not require and to "salt" the generation with additional factors such as user input and other environmental noise such as network traffic.  A good random number generating algorithm will be able to produce different results based on the environment because of the environment.  Take the environment away and the algorithm will be back to it's basic predictable state.

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.
Avatar of grg99
grg99

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
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.
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 ().
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
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();
}
ASKER CERTIFIED SOLUTION
Avatar of KurtVon
KurtVon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial