Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / How Are 'Random' Numbers Generated?

Author
Message
Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 13th Aug 2011 06:43
So i know that computers cant generate genuinely random numbers, but i know there are calculations to produce numbers that SEEM random, even though they are not. What are these calculations? Thanks!

Irrigation of the land with seawater desalinated by fusion power is ancient. It's called rain.
old_School
15
Years of Service
User Offline
Joined: 29th Aug 2009
Location:
Posted: 13th Aug 2011 06:51
Here is my Tic Tac Toe game source code. I had a "Guess My Number" game but I lost the source code sorry. That would of told you how to setup a randomizer. But it sounds like your looking basic game code anyway. This is for Visual Basic so may need conversion from Vb to DB but should give you a good idea.



Hopw this helps
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 13th Aug 2011 07:09 Edited at: 13th Aug 2011 07:11
Quote: "Here is my Tic Tac Toe game source code."


I don't think he wants to know how to code it and don't take this the wrong way but you really need learn about arrays and FOR/NEXT loops. For Darkbasic its as simple as RANDOMIZE TIMER().

Here's what wiki has to say about random number generation:
http://en.wikipedia.org/wiki/Random_number_generation

Edit: Kevin won.

Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 13th Aug 2011 08:47
@old_school Yah, thanks for trying to help! But yah, as the others said i know how to code that already...

@Kevin & Grog i will look at those two very different links!

Irrigation of the land with seawater desalinated by fusion power is ancient. It's called rain.
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 13th Aug 2011 14:06
Also, it's worth mentioning how the standard random functions work on computers. A precalculated table of IIRC 32768 numbers with values from 0 to 32767. When you seed the generator, you use that number to find an item in the table. The value of this item is returned and is used as the index for the next item. (The table is specially made to appear both random, and so that you never end up looping around a small number of items). The table is also standard across most platforms so you can be sure that if you seed with the same number you will generate the same set of results.

[b]
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th Aug 2011 15:17
For the purposes of most games you can use RANDOMIZE TIMER() at numerous intervals to continually reseed the generator. You are highly unlikely to get the same 10 numbers in succession (0.00003% chance) and if you do, it's even more unlikely you'll remember it.

Only if you are looking to generate an array of hundreds of "random" numbers do you need to start getting clever. Then, you may start to see patterns in the sequence. For example if you need 3000 numbers then you are taking 10% of the predetermined sequence and it may become more apparent.

Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 13th Aug 2011 16:09 Edited at: 13th Aug 2011 16:13
Quote: "The table is also standard across most platforms so you can be sure that if you seed with the same number you will generate the same set of results"


Not really:

Quote: "there's no guarantee that rand() is implemented in any specific way, the standard only requires that rand() returns pseudo-random numbers between 0 and RAND_MAX. Even the RAND_MAX value may be different between different implementations."


Apparently GNU C defines RAND_MAX as 2147483647, while for Microsoft's implementation, RAND_MAX is defined as 32767.

It also doesn't seem to be implemented with a lookup table.

old_School
15
Years of Service
User Offline
Joined: 29th Aug 2009
Location:
Posted: 13th Aug 2011 16:52
Lol Jeff thats a big differnence in numbers.
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 14th Aug 2011 00:25 Edited at: 14th Aug 2011 01:16
Quote: "Apparently GNU C defines RAND_MAX as 2147483647, while for Microsoft's implementation, RAND_MAX is defined as 32767.

It also doesn't seem to be implemented with a lookup table."


Ah my bad, I thought I remembered there being tables involved. However, the C99 standard DOES suggest a portable implementation of rand() and srand() using 32767 as RAND_MAX.

Quote: "The following functions define a portable implementation of rand and srand.
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
next = seed;
}"


[b]
ionstream
20
Years of Service
User Offline
Joined: 4th Jul 2004
Location: Overweb
Posted: 14th Aug 2011 01:45
The Mersenne Twister is a popular and more complicated psuedorandom number generator, implemented by Python and various other languages.

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

Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 22nd Aug 2011 07:13
So my apologies for bumping this relatively old thread but for my console, im thinking about using a microphone or maybe an antenna with no tuning circuit to generate random numbers. Would this work well? I can imagine it would as almost all sounds that the microphone would pick up would be at least somewhat random or if if i used the antenna method, the giant conglomeration of radio signals in the air would surely combine together to create a very randomized signal. I would just need to use an analog to digital converter and write the results into a buffer. Would this work well? Thanks!

Irrigation of the land with seawater desalinated by fusion power is ancient. It's called rain.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 22nd Aug 2011 07:27
yup. Google "Hardware random number generator" and you'll probably get a lot of results.

Although, the usefulness of that is limited, and it would require constant tweaking to get quality results (for example, a microwave going on and off might effect results)


Why does blue text appear every time you are near?
Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 22nd Aug 2011 07:37 Edited at: 22nd Aug 2011 07:46
Thanks!
Quote: "for example, a microwave going on and off might effect results"
Definitely something to consider!

Edit:
Sounds like i might use the antenna approach or i might use thermal noise from a resistor or have an LED shining on a photo diode, or maybe even a current through an avalanche diode.

Irrigation of the land with seawater desalinated by fusion power is ancient. It's called rain.
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 22nd Aug 2011 08:02
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 22nd Aug 2011 08:26
@ Kevin Picone:

I was just thinking the same thing.


@ Dark basic dude79:

There's really no point to making a true random generator. The pseudo-random generators we have are just "random" enough so the user doesn't experience the same random numbers. The cool thing about pseudo-random numbers is we can get them to pick exactly the same numbers if we want. Things like repeating a "random" level that you liked and making encryption/decryption routines that use "random" numbers are a couple of the benefits of pseudo-random numbers.

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 22nd Aug 2011 08:59
a hardware random number generator isn't even a true random number generator!


Why does blue text appear every time you are near?
Eminent
14
Years of Service
User Offline
Joined: 15th Jul 2010
Location:
Posted: 22nd Aug 2011 20:57
http://xkcd.com/221/

Durrrrr.


Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 23rd Aug 2011 03:05
Ahhh, good points guys! @Eminent ??

Irrigation of the land with seawater desalinated by fusion power is ancient. It's called rain.

Login to post a reply

Server time is: 2025-05-20 21:04:34
Your offset time is: 2025-05-20 21:04:34