hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Random Number Generation
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Random Number Generation

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 10-18-2006, 04:30 PM
GavinPearce GavinPearce is offline
Newbie
 
Join Date: Dec 2003
Location: Windsor, UK.
Posts: 21
*

Random Number Generation


I was bored last night and decided to teach myself something. I had always wondered how a logical machine like a computer can generate a truly random number, so I done a little research.

Just thought I'd share my findings on the off chance that I wasn't the only person in the world who was wondering!

In basic - there are two types of computer generated random numbers:


1st
You could set a device to monitor a random natural event - a common example tends to be to place a piece of radioactive material in front of a Geiger counter with Geiger counter connected to a computer.

Since radioactive decay is random, the Geiger counter could create truly random numbers.

This approach is pretty rare, for the obvious (I hope) reason that many people do not have Geiger counters connected to their lovely machines.

2nd
You can do the much more common approach - and generate an almost random number, as per the case with most computers.

In a simple terms a formula that generates what is known as a 'pseudo-random number' is created. The idea of the formula is for it to produce a string of numbers that would look random to anyone who did not know the formula itself.

An example of a simple random-number formula from the book "The C Programming Language," by Kernighan and Ritchie:

Code:
int rand() 
{
  random_seed = random_seed * 1103515245 +12345; 
  return (unsigned int)(random_seed / 65536) % 32768; 
}
This formula assumes the existence of a variable (start number) called random_seed, which is initially set to some number. The random_seed variable is multiplied by 1,103,515,245 and then 12,345 gets added to the product and some more lovely mathmatical dividing and adding up. You then get out your first almost random value. Next time the script is run random_seed is then replaced by this new value creating an even more random value.

However can you see the problem here? You need a default value to start the formula. All you've done so far is change a few numbers around. Nothing random about it. If you always used the same value at the start you would always return the same numbers in the same order. So you need a random number to start of the random number sequence?? This was the bit that always used to stump me.

The simple answer is.....

It's impossible to create a truly random number for this value (unless you use the first method listed above) - what most machines resort to is second best - they convert the current date and time into an integer (for example the number of seconds or even miliseconds since 1st Jan 1970) and use this as the magic starting value for the seed.

They then have an always changing number to start off the formula, and hence always generate a different set of values each time the forumla is run!


So there you have it, computers and almost random, random numbers.

__________________
British Airways Silver Wing Sailing Club
All welcome! - http://www.silverwing.org.uk

Reply With Quote


Sponsored Links
  #2  
Old 10-19-2006, 06:09 AM
John[H4Y] John[H4Y] is offline
Nose runs, feet smell?
 
Join Date: Feb 2002
Location: Vestal, NY
Posts: 1,343
Interesting huh? And if you seed a random number generator with a value based on the exact time, technically, it could be considered seeding with a random, unpredictable event. In the case that the computer pulling the time information connects to a time server, which pulls data from an atomic clock, the time may not ALWAYS be what you expect. Large earthquakes such as the one that caused the tsunami in Indonesia, and meteors can throw off the Earth's time. And as of yet, we have no 100% way of predicting these types of events.

__________________
H4Y Technologies LLC Check out our new picture gallery!
"Smarter, Cheaper, Faster" - SMB, Reseller, VDS, Dedicated, Colo hosting done right.

Los Angeles, CA (Peer 1) ZERO DOWNTIME and Scranton, PA 99.9% Uptime Dedicated and Colo.
**http://h4y.us**
Voice: (866)435-5642. *** Email: askus at host4yourself d0t com

Reply With Quote
  #3  
Old 10-19-2006, 06:37 AM
GavinPearce GavinPearce is offline
Newbie
 
Join Date: Dec 2003
Location: Windsor, UK.
Posts: 21
Indeedy.

I rather wish I could attach a device to my postmen, and the log the time he turns up. Then you'd get a truly random number! Grr....

__________________
British Airways Silver Wing Sailing Club
All welcome! - http://www.silverwing.org.uk

Reply With Quote
Sponsored Links
  #4  
Old 10-19-2006, 06:45 AM
John[H4Y] John[H4Y] is offline
Nose runs, feet smell?
 
Join Date: Feb 2002
Location: Vestal, NY
Posts: 1,343
Makes me wonder what the approved way for online casinos and poker rooms would be for generating random cards. Apparently, there is some industry standard they all use.

__________________
H4Y Technologies LLC Check out our new picture gallery!
"Smarter, Cheaper, Faster" - SMB, Reseller, VDS, Dedicated, Colo hosting done right.

Los Angeles, CA (Peer 1) ZERO DOWNTIME and Scranton, PA 99.9% Uptime Dedicated and Colo.
**http://h4y.us**
Voice: (866)435-5642. *** Email: askus at host4yourself d0t com

Reply With Quote
  #5  
Old 10-19-2006, 09:13 AM
dspillettt dspillettt is offline
Aspiring Evangelist
 
Join Date: Jan 2004
Location: York, UK
Posts: 371
If you need some "true" random numbers, have a look at http://random.org/

I wouldn't use it for large numbers of random numbers (I'd consider that to be misuse, though the site seems to suggest they aren't too bothered about that sort of tihng) but I have considered using it in a app to generate a truely random seed for a good arithmetic psuedo-random generator.

Reply With Quote
  #6  
Old 10-19-2006, 09:37 AM
GavinPearce GavinPearce is offline
Newbie
 
Join Date: Dec 2003
Location: Windsor, UK.
Posts: 21
I'm not to sure random.org is actually that random.

It works off atmospheric noise received by a radio tuner on a frequency no one transmits to.

I don't believe atmospheric noise to actually be that random, it's again logical. It's made up of thousands of signals that are all created somewhere, you could predict the event if you studied in enough detail the noise itself and where it comes from. (A lot is from space - again however their are reasons why this happens). Indeed I agree it makes pseudo-random number, but not so sure about being truly random.

__________________
British Airways Silver Wing Sailing Club
All welcome! - http://www.silverwing.org.uk

Reply With Quote
  #7  
Old 10-19-2006, 09:46 AM
bigfan bigfan is offline
WHT Addict
 
Join Date: Nov 2005
Posts: 123
I wouldn't look to casinos for examples of true randomness, or even near-randomness. Their machines are all programmed to give them a winning edge, the size of which varies among casinos and jurisdictions.

Reply With Quote
  #8  
Old 10-19-2006, 06:12 PM
null null is offline
Web Hosting Master
 
Join Date: Sep 2002
Location: Illinois
Posts: 2,305
Here's how PokerStars generates random cards:

http://www.pokerstars.com/poker/room/features/security/

__________________
How's my programming? Call 1-800-DEV-NULL

Reply With Quote
  #9  
Old 10-19-2006, 06:53 PM
Lightwave Lightwave is offline
Web Hosting Master
 
Join Date: Apr 2003
Location: San Jose, CA.
Posts: 1,616
http://www.lavarand.org/

Landon is a cool guy... I've always liked how he put "A job with an unspecified branch of the US Federal Government, 1979-82" (cough NSA cough) on his resume :p

Reply With Quote
  #10  
Old 10-19-2006, 07:21 PM
John[H4Y] John[H4Y] is offline
Nose runs, feet smell?
 
Join Date: Feb 2002
Location: Vestal, NY
Posts: 1,343
Quote:
Originally Posted by GavinPearce
I'm not to sure random.org is actually that random.

It works off atmospheric noise received by a radio tuner on a frequency no one transmits to.

I don't believe atmospheric noise to actually be that random, it's again logical. It's made up of thousands of signals that are all created somewhere, you could predict the event if you studied in enough detail the noise itself and where it comes from. (A lot is from space - again however their are reasons why this happens). Indeed I agree it makes pseudo-random number, but not so sure about being truly random.
There is background noise and radio interference from the big bang, sunspots, solar flares, etc. That would be QUITE random or at least WAYYYY too complicated to predict for any mere human.

__________________
H4Y Technologies LLC Check out our new picture gallery!
"Smarter, Cheaper, Faster" - SMB, Reseller, VDS, Dedicated, Colo hosting done right.

Los Angeles, CA (Peer 1) ZERO DOWNTIME and Scranton, PA 99.9% Uptime Dedicated and Colo.
**http://h4y.us**
Voice: (866)435-5642. *** Email: askus at host4yourself d0t com

Reply With Quote
  #11  
Old 12-08-2006, 11:36 AM
GavinPearce GavinPearce is offline
Newbie
 
Join Date: Dec 2003
Location: Windsor, UK.
Posts: 21
But still perdictable. :d

__________________
British Airways Silver Wing Sailing Club
All welcome! - http://www.silverwing.org.uk

Reply With Quote
  #12  
Old 12-08-2006, 02:02 PM
Premier Premier is offline
Web Hosting Master
 
Join Date: Apr 2003
Location: Canada
Posts: 587
Quote:
Originally Posted by GavinPearce
Indeedy.

I rather wish I could attach a device to my postmen, and the log the time he turns up. Then you'd get a truly random number! Grr....
That would have worked for me too at the last place I was living.



When I need random numbers in a script, I start with a seed that is determined by some calculations done with time and visitors IP. That way I'm starting with 2 unrelated variables to create the starting point.

__________________
Mike
cPanel/WHM scripts at Premier Website Solutions (all your website needs)
Support young figure skaters in training. juniorskaters.com

Reply With Quote
  #13  
Old 12-08-2006, 05:19 PM
folsom folsom is offline
WHT Addict
 
Join Date: Oct 2004
Posts: 104
I have always liked the way that AOLServer does random seeds.

The cool part is that the entropy comes from starting a counter in a different thread then grabbing the value of and resetting the counter at somewhat random times while it is running.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Lead Generation Part 4 – Go it Alone Blog 2013-03-25 09:16:50
Lead Generation Part 1 – Form Fills Blog 2012-11-16 09:03:48
Sign Up for Wednesday's Webinar on Evaluating Lead Generation Opportunities Blog 2012-09-11 11:29:35
VIDEO: ProfitBricks CEOs Describe the Next Generation Cloud Whir Tv 2012-10-16 17:05:53
Peek Inside the Next Generation of OpenStack Cloud Webinars 2012-09-04 13:56:34


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?