Random number without repeat as3

We’re using the random object for many things, by its nature it may repeat a number, but sometimes we don’t want to get the same number more than once, for example when showing a random image out of an array of images, or jumping to a random frame in a MovieClip. Currently there is no ‘unique’ function in Actionscript, you have to write your own solution.

Here is my class for generating random numbers:
Download: RandomPlus.as.

Usage:
Create an instance of RandomPlus, defining the numbers scope (start & end numbers), including negative values. in case only one parameter is passed the RandomPlus object will assume it is the end number and that the start number is zero:
var rp:RandomPlus = new RandomPlus( end:Number, start:Number = 0 );

After that you can use ‘getNum’ public function that will return a random number within the scope:
rp.getNum(); // returns a Number

You can use public var ‘len’ to retrieve the length of the scope in case you want to use it for your function:
rp.len; // returns a Number

*The object will return unlimited amount of numbers, each time the entire scope was returned it will reshuffle the items so it will continue to return random numbers within the scope, in an ever-changing order.

Let me know if you liked it (or if you have suggestions), enjoy :)

Join the Conversation

12 Comments

  1. very good article.thank you for this great article.
    i was checking for the same.
    i have one more doubt.can i get random of two three or four numbers not within rage?
    like if i give 2,7,8 then i have to get either 2 or 7 or 8.
    if i give 10,4,8,6 i have to get any one of the numbers…..

  2. sweet! thanks for sharing.

    here’s my suggestion, add an optional parameter which limits the number of generated numbers before reshuffling.

    say my start is 0 and end is 19, but i only want 16 random numbers. :)

  3. hey i have 2 things to say.

    first of all great script seriusly this will help alot in a learning game i am making. no wories i have already put you on the special thanks list. and the game is free for use when it’s done sort of a litle project. so big big thanks.

    second i ran into a smal problem
    when i use the code in a function that should pik a number and place stones on the game board.

    var rp:RandomPlus = new RandomPlus(end:Number, start:Number = 0);

    the debug sas tath this line of code gives the following error.

    Scene 1, Layer ‘functions’, Frame 1, Line 120 1084: Syntax error: expecting rightparen before colon.

    i made a clas file in the same folder and pasted the whole class RandomPlus in it and saved it as RandomPlus.class.

    am i doing somthing wrong ?

    will

    1. Hey Will,

      I think your problem is defining the scope – you should define ‘start’ & ‘end’ like this:

      var startNum:Number = 0;
      var endNum:Number = 10;
      var rp:RandomPlus = new RandomPlus(endNum, startNum);

      Now you can start getting random numbers between 0 to 10 like this:

      rp.getNum();

      Hope this solves your problem, good luck with your project!
      A.

  4. This class is quite useful, very! Thank you so much for this. HOWEVER I’ve got a question, maybe you can help me or improve the class even more…
    As the random numbers start all over again and all the values are available again, there’s a chance the last number selected it’s the same as the new one and this is a problem… It’s a little hard to explain…

    Here’s an example: Let’s suppose the start it’s 0 and the end 3. With RandomPlus object we’ll get this:
    3,1,2,0
    2,1,0,3
    0,3,2,1
    1,0,2,3

    Look at the last two rows: In the third row the last numer is one… and in the fourth row the first number is one too!!! So there are two same numbers below each other. Now am I clear?
    Have you got an idea how to avoid this to happen??
    THANKS!! =)

    1. They are not below each other. It’s a new sequence. Everything is fine with algo.

Leave a comment

Your email address will not be published. Required fields are marked *