1. Dear Guest, if you are not receiving a forgot pw email then use the contact form at the bottom of the page, or email [email protected]
    Dismiss Notice
  2. Dismiss Notice
  3. Dear Guest, some forum sections are hidden for you. Register for FREE for unlimited access.
    Dismiss Notice

[PHP] Pseudo-Random Number Generation

Discussion in 'Tutorials' started by JfN, Mar 2, 2015.

Share This Page

  1. JfN

    JfN Well-Known Member

    Joined:
    Jan 18, 2012
    Messages:
    4,844
    Likes Received:
    28
    Trophy Points:
    48
    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:​
    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.
     
  2. Ally Mac

    Ally Mac -

    Joined:
    Nov 11, 2014
    Messages:
    449
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  3. JfN

    JfN Well-Known Member

    Joined:
    Jan 18, 2012
    Messages:
    4,844
    Likes Received:
    28
    Trophy Points:
    48
    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