1. Dear Guest, if you are not receiving a forgot pw email then use the contact form at the bottom of the page. Bare in mind this is not monitored 24/7. Join our discord for help with account issues/queries. Join our discord: by clicking HERE.
    Dismiss Notice
  2. Dismiss Notice
  3. Dear Guest, some forum sections are hidden for you. Register for FREE for unlimited access.
    Dismiss Notice

[AS3] Pseudo-Random Number Generation

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

Share This Page

  1. JfN

    JfN Well-Known Member

    Joined:
    Jan 18, 2012
    Messages:
    4,849
    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 ActionScript 3 (AS3), which is the language used to create Flash/AIR applications.​

    Prerequisites: An environment designed to code, debug, and compile Flash projects.​
    Highly recommended: FlashDevelop. This guide will be mainly focused on FlashDevelop.​

    Let's start with the example in the original post:​
    Rather than creating a button, I've decided it'd be easier for everyone if I were to use a timer - especially those who aren't experienced in AS3. So let's begin!​

    Preparing the environment
    1. After opening FlashDevelop, click the 'Project' tab, then 'New Project'. At this point click 'AS3 Project'.
    2. Name the project something like 'Pseudo-Random Number Generator', or whatever.
    3. Pick your own location, but don't create a new folder for the project, tick 'Create directory for project' instead.
    4. Click 'ok'.
    5. On the 'Project panel',  open the 'src' folder by either double clicking or pressing the tickbox next to it.
    6. Double click Main.as - and you're ready to begin!

    Creating the Randomiser
    Paste the following code, and I'll explain it piece by piece after.​
    Code:
    package
    {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.events.TextField;
    import flash.events.Timer;
    
    public class Main extends Sprite
    {
    public var generatedNumber:TextField = new TextField();
    public var generationTimer:Timer = new Timer(100,0);
    
    public function Main():void
    {
    addChild(generatedNumber);
    generationTimer.start();
    generationTimer.addEventListener(TimerEvent.TIMER, generateNumber);
    }
    
    public function generateNumber(e:TimerEvent)
    {
    var currentTime:Date = newDate();
    generatedNumber.text = "Number: " + Math.cail((((currentTime.milliseconds /15) * 1.59) + 2));
    }
    }
    }

    public var generatedNumber:TextField = new TextField();
    This is just creating a TextField for the randomly generated number to show up on.

    public var generationTimer:Timer = new Timer(100,0);
    This is creating a timer that will tick every 0.1 seconds (100 milliseconds).

    addChild(generatedNumber);
    This tells Flash to show the TextField in the application.

    generationTimer.start();
    This starts the timer.

    generationTimer.addEventListener(TimerEvent.TIMER, generateNumber);
    This is an event listener. It basically listens out for an event, and the event it listens out for is the timer event. When the timer ticks, it triggers the function 'generateNumber'.

    var currentTime: Date = newDate();
    This is how we get the current date and time.

    generatedNumber.text = "Number: " + Math.ceil((((currentTime.milliseconds /15) * 1.59) + 2));
    This is the basic Pseudo-Random Number generator that is triggered every 0.1 seconds. It takes the current time in milliseconds, divides it by 15, multiplies it by 1.59, adds 2, and uses "Math.ceil()" to round it up!

    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!​
     

Top