[AS3] Pseudo-Random Number Generation


Well-Known Member
Rating - 100%
49   0   0
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:​
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 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.​
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
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).

This tells Flash to show the TextField in the application.

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