[PHP] Pseudo-Random Number Generation

JfN

Well-Known Member
Rating - 100%
49   0   0
#1
This is an extension of the thread: http://primeforums.net/Thread-Randomisers-in-programing-Tutorial
lf you haven't read it, you probably should to understand the difference between PRN's and TRN's.​

In this extension of Randomisers in programing - I will be showing Prime Forums an example of a Pseudo-Random Number generator written in Pre-Hypertext Processor (PHP), which is a language used to add dynamic functions and database interaction to websites.​

Prerequisites: An environment designed to code and run PHP files.​
Highly recommended: Notepad++ to code, and a HTTP server (Apache, IIS, XAMPP, etc) with PHP configured. Any web host will have this.​

Let's start with the example in the original post:​
JfN said:
Workable example: Create a button in whatever programming language you desire, and add a text field next to it to display a number. Now, whenever the button is pressed - program your application/website to get the time in seconds, then divide it by 15, multiply it by 1.59 and add 2, then round up to the first whole number. Put the number in the text field. This is a very basic pseudo-random number.
Rather than creating a button, I've decided it'd be easier if I were to just use a link that would refresh the page in order to re-run the randomiser - especially for those who aren't experienced in web development. So let's begin!​

Preparing the environment
  1. Find the root directory for your http server. Most are in the C: directory under a folder name 'www' or 'htdocs'.
  2. Create a file called 'randomiser.php' in this folder.
  3. Open Notepad++.
  4. Click the 'File' tab, then click 'Open' and find the "randomiser.php" file you just created.

Creating the Randomiser
Paste the following code, and I'll explain it piece by piece after.​

Code:
<?php
$getMilliseconds = round(microtime()*100);
$randomiseMilliseconds = round((($getMilliseconds / 15) * 1.59) + 2);

echo $randomiseMilliseconds;
?>
<br><a href="/randomiser.php">Generate new number!</a>

$getMilliseconds = round(microtime()*100);
This is how we get the current time in milliseconds.

$randomiseMilliseconds = round((($getMilliseconds / 15) * 1.59) + 2);
This is the basic Pseudo-Random Number generator that is triggered every time the page loads. It takes the current time in milliseconds, divides it by 15, multiplies it by 1.59, adds 2, and uses "round()" to round it up!

echo $randomiseMilliseconds;
This displays the generated number on the page.

<br><a href="/randomiser.php">Generate new number!</a>
This basically is a link that refreshes the page, generating a new number.

The numbers that pop up will seem pretty fucking random to the normal eye, and once again this is why it's known as a "Pseudo-Random Number".

In the future I will post a downloadable example. Feel free to ask any questions!
[hr]
P.S. - I'd also like to add that PHP already has a built-in PRN, which is rand(); - the above method was created purely for educational purpose and is in no way more efficient than the built-in function.

Although you could probably use this guide to make a much better randomiser than rand(), this is far from it.
 
Rating - 0%
0   0   0
#2
Interesting approach to generating non-vital random numbers, but would it not be better to involve exponentials? The large difference in complexity for only a minor hit in processing usage, surely it would be beneficial? 

Would that approach even work? I can't say I've worked very much with PSNGs. 

Edit: Just noticed the last part. In the future do you intend to expand into these more beneficial methods?
 

JfN

Well-Known Member
Rating - 100%
49   0   0
#3
Ally Mac said:
Interesting approach to generating non-vital random numbers, but would it not be better to involve exponentials? The large difference in complexity for only a minor hit in processing usage, surely it would be beneficial? 

Would that approach even work? I can't say I've worked very much with PSNGs. 

Edit: Just noticed the last part. In the future do you intend to expand into these more beneficial methods?
This is far from a commercially-viable method due to high potentiality of exploitation, but for the sake of education I figured it'd be a decent read.

I could do some more complex methods after I've had a chance to cover more languages using this basic method. I'd also very much like to try an experiment with the TRN theory I discussed in my other thread when I have the funding to do so :).

Thanks a lot for the reply!
 

Top