Skip to main content
Inspiring
February 21, 2011
Answered

Random number in range generator plz help!

  • February 21, 2011
  • 1 reply
  • 2138 views

hey hope someone can help me out, im sure its an easy one:

var tstArray:Array = new Array(1,2,3);

for (var a:uint=0; a<tstArray.length; a++)

{

var x:Number = Math.floor((Math.random()*tstArray));

trace(x);

tstArray.splice(a,1);

}

i have this code but it doesnt do exectply what i want it to do. Im trying to generate a signle random number per click between 1 and 3 and when all numbers have been used up, remove this click listener.

if u can help it'll be much appriciated

thx pavel

This topic has been closed for replies.
Correct answer kglad

this is how the wntire code looks like and its being pulled into another class when a click occurs:

package

{

*/import flash.utils.Timer;

import flash.display.Sprite;

import flash.events.MouseEvent;

import flash.display.MovieClip;

import flash.events.Event;

import flash.events.*;

import flash.media.Sound;

import flash.events.TimerEvent;

import flash.media.SoundChannel;

import flash.media.SoundTransform;*/

public class farm_Part1

{

public function randomAnimal()

{

var tstArrray:Array = [1,2,3];

tstArray.shuffle();

///////////////////////////// change nothing below /////////////////////////////////////////

function shuffle(a:Array)

{

var p:int;

var t:*;

var ivar:int;

for (ivar = a.length-1; ivar>=0; ivar--)

{

p=Math.floor((ivar+1)*Math.random());

t = a[ivar];

a[ivar] = a

;

a

= t;

}

}

}//randomAnimal

}//class

}//package


use:

var fp1:farm_part1=new farm_part1();

and any time you want to randomize an array use:

var tstArray:Array = fp1.randomizeF([1,2,3]);


package

{

*/import flash.utils.Timer;

import flash.display.Sprite;

import flash.events.MouseEvent;

import flash.display.MovieClip;

import flash.events.Event;

import flash.events.*;

import flash.media.Sound;

import flash.events.TimerEvent;

import flash.media.SoundChannel;

import flash.media.SoundTransform;*/

public class farm_Part1

{

public function randomizeF(a:Array):Array

{

a.shuffle();
return a;
}

///////////////////////////// change nothing below /////////////////////////////////////////

function shuffle(a:Array)

{

var p:int;

var t:*;

var ivar:int;

for (ivar = a.length-1; ivar>=0; ivar--)

{

p=Math.floor((ivar+1)*Math.random());

t = a[ivar];

a[ivar] = a

;

a

= t;

}

}

}//randomAnimal

}//class

}//package

1 reply

kglad
Community Expert
Community Expert
February 21, 2011

var tstArrray:Array=[1,2,3];

tstArray.shuffle();

now, loop through your shuffled array from index 0 to its end.

///////////////////////////// change nothing below /////////////////////////////////////////

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

/////////////////////////////////////////////////

pa-pavelAuthor
Inspiring
February 21, 2011

hey man, thx for the fast reply.

i get this error:

TypeError: Error #1006: shuffle is not a function.

in using the code inside a class in a public function could that be the problem?

thx

kglad
Community Expert
Community Expert
February 21, 2011

the function shuffle() needs to be in the scope where it's called.