Skip to main content
New Participant
March 25, 2011
Answered

Need random number 1-24, no repeats until all 24 have been generated!

  • March 25, 2011
  • 1 reply
  • 1914 views

I am creating an app that flips through a series of 24 flashcards.

I want my "Go To Random" button to navigate to a random frame number between 2 and 25, but with no repeats until it has already navigated once to each of the frames in between.

In other words the user doesn't get any repeats until he has already gone through all 24 cards - and then the shuffle repeats.

New to flash, thanks.

This topic has been closed for replies.
Correct answer Ned Murphy

I don't like handing out solutions when someone has nothing to show for effort because I feel it doesn't do anything to help someone learn... but the level of effort to prepare this was low, so...

// create the array

var nums:Array = new Array();

// fill the array 1,2,3,4....

for(var i:uint=1; i<25; i++){
nums.push(i);
}

// for shuffling the array

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;
    }
}

// shuffle the array

shuffle(nums);

// set up the controls for the button

var count:uint = 0;

gotoRandomBtn.addEventListener(MouseEvent.CLICK, gotoFrame);

function gotoFrame(evt:MouseEvent):void {
    gotoAndStop(nums[count]+1);
    count++;
    if(count == 25){
        shuffle(nums);
        count = 0;
    }
}

This is untested.  If you have a problem... try to resolve it yourself (that's how I learn most of this stuff)

1 reply

Ned Murphy
Brainiac
March 25, 2011

Create an array of the frame numbers, shuffle it, and then walk thru it in its shuffled order.

New Participant
March 25, 2011

Thanks Ned

...plus, after program has have walked through the entire array, it needs to create a new shuffle and start again.

Conceptually I understand this is what needs to happen, but I don't know how to script it.  Haven't learned to write arrays yet.. trying to teach myself.

Ned Murphy
Ned MurphyCorrect answer
Brainiac
March 25, 2011

I don't like handing out solutions when someone has nothing to show for effort because I feel it doesn't do anything to help someone learn... but the level of effort to prepare this was low, so...

// create the array

var nums:Array = new Array();

// fill the array 1,2,3,4....

for(var i:uint=1; i<25; i++){
nums.push(i);
}

// for shuffling the array

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;
    }
}

// shuffle the array

shuffle(nums);

// set up the controls for the button

var count:uint = 0;

gotoRandomBtn.addEventListener(MouseEvent.CLICK, gotoFrame);

function gotoFrame(evt:MouseEvent):void {
    gotoAndStop(nums[count]+1);
    count++;
    if(count == 25){
        shuffle(nums);
        count = 0;
    }
}

This is untested.  If you have a problem... try to resolve it yourself (that's how I learn most of this stuff)