rose
  • Hello all..

    I have this simple bit of code which is used regularly in my movie...

    minNum = 1;
    maxNum = 200;
    number = Math.ceil(Math.random() * (maxNum - minNum + 1)) + (minNum - 1);

    Is it possible to add something to this code to stop the same number being called more than once?

    David


  • You can thank the forums by not cross-posting next time! :)


  • Here is another method, though it is a bit longer: var minNum:Number = 1;
    var maxNum:Number = 200;
    var numbers:Array = new Array();
    var count:Number = 0;
    for (i=minNum; i<=maxNum; i++) {
    numbers[i-1] = i;
    }
    numbers.sort(shuffle);
    this.onMouseDown = function():Void {
    trace(getNum());
    };
    function shuffle():Number {
    return Math.floor(Math.random()*3)-1;
    }
    function getNum():Number {
    if (numbers[count] == numbers[numbers.length-1]) {
    numbers.sort(shuffle);
    count = 0;
    }
    return numbers[count++];
    }

    I don't remember where I first saw the shuffle function, but it isn't my creation.


  • Cheers for all of the help...

    Much appreciated...

    David


  • hmmm, there are several ways to do this I'm sure but the way I've done this in the past is to generate an array containing all your numbers - then randomly pick a number from the array, after retreiving that number, splice that number from the array.



    var numArr:Array = ;
    var minNum:Number = 0;
    var maxNum:Number = 200;

    // popluate array
    for(var i:Number=0; i<=maxNum; i++) {
    numArr.push(i);
    }

    // function to retreive number
    var retreiveNum = function():Number {
    var max:Number = numArr.length-1;

    // if max is less than 0, then all numbers from the array have been picked
    if(max >= 0) {
    var num:Number = minNum + Math.floor(Math.random()*(max+1-minNum));
    var arNum:Number = numArr[num];
    numArr.splice(num,1);
    return arNum;
    }
    }

    _root.onMouseDown = function() {
    trace(retreiveNum());
    }


  • lol







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Random Number Question , Please add it free.
    • edit