views:

523

answers:

6

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?

A: 

sorry but...

http://letmegooglethatforyou.com/?q=random+number+generation

Triptych
Now that's funny.... :)
Roy
Sure... but one of the purposes of this site is to cause such collective wisdom to be gathered in one place... here.
Software Monkey
-1 for not really helping
Robert Gould
that's also the purpose of wikipedia, which will have more better information than whatever we end up with here on such a well-studied topic. I say teach a man to fish.
Triptych
Random fact, I just looked up letmegooglethatforyou's estimated adsense money. He makes about $112 / day, and that is double what he was making 30 days ago. Once his traffic peaks he could easily make 6 figures a year, all from such a simple thing.
Simucal
Not helpful - I wish I'd see less posts like this on here. -1
barfoon
haha. this has three upvotes and four down votes.
Triptych
@Simucal...hmmm maybe @Triptych is the guy!! And +1, I would google something like this too.
kenny
Overwriting the post? Is that really necessary?
bdonlan
+3  A: 

http://en.wikipedia.org/wiki/Random_number_generator

Describes the different types of random number generators and how they are created.

Soviut
+3  A: 

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

Mystic
+2  A: 

I assume you mean pseudo-random numbers. The simplest one I know (from writing videogames games back on old machines) worked like this:

seed=seed*5+1;

You do that every time random is called and then you use however many low bits you want. *5+1 has the nice property (IIRC) of hitting every possibility before repeating, no matter how many bits you are looking at.

The downside, of course, is its predictability. But that didn't matter in the games. We were grabbing random numbers like crazy for all sorts of things, and you'd never know what number was coming next.

Do a couple things like this in parallel, and combine the results. This is a linear congruential generator.

Nosredna
Those are horrible choices of a, c, and m!
derobert
Haha. Yes, but the first time I ever saw that routine was on a 1 MHz machine. :-)
Nosredna
+1  A: 

I want to generate random numbers manually.

I would roll some dice ... (SCNR)

More seriously, I roll a pair of dice for creating passwords.

starblue
Obligatory link: http://xkcd.com/221/
Adam Rosenfield
A: 

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;
}
Adam Rosenfield