-1

I have a random number variable defined as below

var rannum:Number = Math.floor(Math.random()*50+1);

Then I have a trigger that calls for a new random number everytime a button is clicked

ranbtn.addEventListener(MouseEvent.CLICK, reran);
function reran (event:MouseEvent):void
{  
  rannum = Math.floor(Math.random()*50+1); 
}

I would like to prevent the same random number from being selected until all the numbers have been selected and then possibly start over? I found a few threads like this one but none of them were specifically what I needed

Koden
  • 353
  • 1
  • 13
  • Depending on how much randomness you need, [this](http://stackoverflow.com/a/20194928/666785) might apply. – sbabbi Mar 11 '16 at 10:15
  • 1
    This is where you should use a custom random number generator. There are random number generators which will provably not repeat a number x in a period of y number generations. Math.random() is not such a random number generator. – Zéychin Mar 11 '16 at 12:43
  • "until all the numbers have been selected" -- all *what* numbers? Between 1 and 50? There are an infinite number of numbers between 1 and 50. You mean all *integers* from 1 to 50 (inclusive)? It would be helpful to say so. :) – Aaron Beall Mar 11 '16 at 18:02

3 Answers3

3

You need to create an array of the possible values and each time you retrieve a random index from the array to use one of the values, you remove it from the array.Here you have an easy example with javascript.

var uniqueRandoms = [];
var numRandoms = 50;
function makeUniqueRandom() {
    // refill the array if needed
    if (!uniqueRandoms.length) {
        for (var i = 0; i < numRandoms; i++) {
            uniqueRandoms.push(i);
        }
    }
    var index = Math.floor(Math.random() * uniqueRandoms.length);
    var val = uniqueRandoms[index];

    // now remove that value from the array
    uniqueRandoms.splice(index, 1);

    return val;

}

I've found another option, You can declare an array of Integers:[1,2,3,4...50] and sort them randomly.

 var sorted:Array = [];

    for(var i:int = 0; i < 50; i++){
        sorted.push(i);
    }

    //I'm making a copy of sorted in unsorted        
    var unsorted:Array = sorted.slice();

    //Randomly sort 
    while(sorted.join() == unsorted.join()){    
        unsorted.sort(function (a:int, b:int):int { return Math.random() > .5 ? -1 : 1; });
    }
Jorge Nieto
  • 119
  • 5
0

If you get a selected num, you can add one until it is not selected.

suzuiyue
  • 156
  • 1
  • 1
  • 12
0
  1. Create a list of integers from 1 to 50.
  2. Pick a random integer from the list and remove it.
  3. When there are no more integers left (after 50 picks), repeat step 1.

Code:

function createRangeOfIntegers(from:int, to:int):Vector.<int> {
    if (from >= to) throw new ArgumentError("Invalid arguments");
    var integers:Vector.<int> = new <int>[];
    for (var i:int = from; i <= to; i++) {
        integers.push(i);
    }
    return integers;
}

function getRandomInteger(integers:Vector.<int>):int {
    var index:int = Math.random() * integers.length;
    var integer:int = integers.splice(index, 1)[0];
    return integer;
}

Example:

// create all the possible integers
var integers:Vector.<int> = createRangeOfIntegers(1, 50);

// select a random integer
var random:int = getRandomInteger(integers);

// When you've selected all integers you can start over
if (integers.length == 0)
    integers = createRangeOfIntegers(1, 50);
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103