Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually generate random numbers [closed]

Tags:

random

I want to generate random numbers manually. I know that every language have the rand or random function, but I'm curious to know how this is working. Does anyone have code for that?

like image 685
Omar Abid Avatar asked Feb 03 '09 05:02

Omar Abid


People also ask

How do I lock a random number generator in Excel?

To stop the RAND or RANDBETWEEN functions from recalculating in one cell, select that cell, switch to the formula bar and press F9 to replace the formula with its value. To prevent an Excel random function from recalculating, use the Paste Special > Values feature.

Can you trick a random number generator?

As you can see, it is completely possible to hack an RNG that's based on a computer program like the ones used in casinos and online games. That's not to say, however, that it is easy. These companies spend a pretty penny to make sure that their games are secure with extensive protocols installed.

How do you generate random numbers without using library functions?

If we want to generate random number between 20 and 30; then we need to mod the random number with it's limit difference (p%(10)), that means (30–20=10). It will find the random the number between 0 to 10 as p will provide a random number.


2 Answers

POSIX.1-2001 gives the following example of an implementation of rand() and srand(), possibly useful when one needs the same sequence on two different machines.

static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
    next = seed;
}
like image 58
Adam Rosenfield Avatar answered Sep 28 '22 03:09

Adam Rosenfield


Have a look at the following:

Random Number Generation

Linear Congruential Generator - a popular approach also used in Java

List of Random Number Generators

And here's another link which elaborates on the use of LCG in Java's Random class

like image 42
Mystic Avatar answered Sep 28 '22 04:09

Mystic