2 Replies Latest reply on May 23, 2006 2:39 AM by 2m

# Random Function

Is there a simple way of producing random numbers without repitition in flash.
I.e. I have a range of numbers I want it to choose randomly from but without withdrawing the same number twice?

Is this a valid method (from a programming point of view): Have an array of the values then every time a position is chosen randomly on the array it is removed (placed elsewhere) so that it cannot be called twice?

Thanks for any help!
• ###### 1. Re: Random
Hi!

Yeah, you could do it that way, or you could do the random process in
advance. Below is some code that might be of use...

function getRandom(a,b):Number {
return Math.round(Math.random()*(b-a))+a;
}

function createRandomNrList(minValue:Number, maxValue:Number):Array {
var L:Array = new Array();
var FinalList:Array = new Array();
var nr = minValue;
while (nr <= maxValue) {
L.push(nr++);
}
trace(L);
while (L.length > 0) {
var r = getRandom(0,L.length-1);
FinalList.push(L.splice(r,1)[0]);
}
return FinalList;
}

function getRandomNumber(RandomNrList:Array):Number {
return Number(RandomNrList.pop());
}

var RandomList = createRandomNrList(20,30);

while (RandomList.length > 0) {
trace(getRandomNumber(RandomList));
}

/Jensen/

"Tolk" <webforumsuser@macromedia.com> wrote in message
news:e4uh52\$duh\$1@forums.macromedia.com...
> Is there a simple way of producing random numbers without repitition in
> flash.
> I.e. I have a range of numbers I want it to choose randomly from but
> without
> withdrawing the same number twice?
>
> Is this a valid method (from a programming point of view): Have an array
> of
> the values then every time a position is chosen randomly on the array it
> is
> removed (placed elsewhere) so that it cannot be called twice?
>
> Thanks for any help!
>

• ###### 2. Re: Random Function
I don't know about valid methods from a programming point of view, but that's what I do in such a case and it has served me fine.