Jump to content

php rand function


nmts

Recommended Posts

Can anyone explain how this function actually works? Is there a way to write your own random function? Not that I need to, but it would be cool to understand it

 

php's rand() function is based on so called pseudo-random numbers.

A pseudo-random number is based on on a formula that would look random to anyone that does not know the formula. And based on a quote from a book covering the topic of random functions, this formula should have the following characteristics:

 

Good numeric distribution, let say you want to produce a random number between 0 and 5 then the formula should produce a equal ammounts of 0 - 1 - 2 - 3 - 4 - 5 over time.

 

You should not be able to predict the numbers, unless you know the formula and the start value used in the formula.

 

The process should not cycle, in other words produce the same set numbers over x number of intervalls.

 

So it's not really random, but it seems random to the user.

 

If you check major online casinos they generate random numbers using nature, most common is using materials that randomly discharges photons to trigger a number, or use radioactive isotops as radio active decay is random by nature you can never predict when a "burst" happens, using this you will achieve "true" randomness by measuring when this happens.

 

*edit*

check php's better random function called: mt_rand() as it's much faster than rand() and uses the Mersenne Twister method to generating numbers.

Edited by krillz
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...