1

I have seen this question for other languages but not for AS3... and I'm having a hard time understanding it... I need to generate 3 numbers, randomly, from 0 to 2, but they cannot repeat (as in 000, 001, 222, 212 etc) and they cannot be in the correct order (0,1,2)...

Im using

for (var u: int = 0; u < 3; u++)
{
    mcCor = new CorDaCarta();
    mcCor.x = larguraTrio + (mcCor.width + 5) * (u % 3);
    mcCor.y = alturaTrio + (mcCor.height + 5) * (Math.floor(u / 3));

    mcCor.gotoAndStop((Math.random() * (2 - u + 1) + u) | 0);           // random w/ repeats
    //mcCor.gotoAndStop(Math.floor(Math.random() * (2 - u + 1) + u));   // random w/ repeats
    //mcCor.gotoAndStop((Math.random() * 3) | 0);                       // crap....
    //mcCor.gotoAndStop(Math.round(Math.random()*u));                   // 1,1,1
    //mcCor.gotoAndStop(u + 1);                                         // 1,2,3

    mcCor.buttonMode = true;
    mcCor.addEventListener(MouseEvent.CLICK, cliquetrio);
    mcExplic.addChild(mcCor);
    trio.push(mcCor);
}

those are the codes i've been trying.... best one so far is the active one (without the //), but it still gives me duplicates (as 1,1,1) and still has a small chance to come 0,1,2....

BTW, what I want is to mcCor to gotoAndStop on frames 1, 2 or 3....without repeating, so THE USER can put it on the right order (1,2,3 or (u= 0,1,2), thats why I add + 1 sometimes there)

any thoughts?? =)

Surtarso
  • 37
  • 6

3 Answers3

2

I've found that one way to ensure random, unique numbers is to store the possible numbers in an array, and then sort them using a "random" sort:

// store the numbers 0, 1, 2 in an array
var sortedNumbers:Array = [];
for(var i:int = 0; i < 3; i++)
{
    sortedNumbers.push(i);
}

var unsortedNumbers:Array = sortedNumbers.slice(); // make a copy of the sorted numbers

trace(sortedNumbers); // 0,1,2
trace(unsortedNumbers); // 0,1,2

// randomly sort array until it no longer matches the sorted array
while(sortedNumbers.join() == unsortedNumbers.join())
{
    unsortedNumbers.sort(function (a:int, b:int):int { return Math.random() > .5 ? -1 : 1; });
}

trace(unsortedNumbers); // [1,0,2], [2,1,0], [0,1,2], etc

for (var u: int = 0; u < 3; u++)
{
    mcCor = new CorDaCarta();
    mcCor.x = larguraTrio + (mcCor.width + 5) * (u % 3);
    mcCor.y = alturaTrio + (mcCor.height + 5) * (Math.floor(u / 3));

   // grab the corresponding value from the unsorted array
   mcCor.gotoAndStop(unsortedNumbers[u] + 1); 

    mcCor.buttonMode = true;
    mcCor.addEventListener(MouseEvent.CLICK, cliquetrio);
    mcExplic.addChild(mcCor);
    trio.push(mcCor);
}   
Marcela
  • 3,728
  • 1
  • 15
  • 21
1

Marcela is right. Approach with an Array is widely used for such task. Of course, you will need to check 0, 1, 2 sequence and this will be ugly, but in common code to get the random sequence of integers can look like this:

function getRandomSequence(min:int, max:int):Array
{
    if (min > max) throw new Error("Max value should be greater than Min value!");
    if (min == max) return [min];
    var values:Array = [];
    for (var i:int = min; i <= max; i++) values.push(i);
    var result:Array = [];
    while (values.length > 0) result = result.concat(values.splice(Math.floor(Math.random() * values.length), 1));
    return result;
}

for (var i:uint = 0; i < 10; i++)
{
    trace(getRandomSequence(1, 10));
}

You will get something like that:

2,9,3,8,10,6,5,1,4,7
6,1,2,4,8,9,5,10,7,3
3,9,10,6,8,2,5,4,1,7
7,6,1,4,3,8,9,2,10,5
4,6,7,1,3,2,9,10,8,5
3,10,5,9,1,7,2,4,8,6
1,7,9,6,10,3,4,5,2,8
4,10,8,9,3,2,6,1,7,5
1,7,8,9,10,6,4,3,2,5
7,5,4,2,8,6,10,3,9,1
Zhlechker
  • 31
  • 3
0

I created this for you. It is working but it can be optimized...

Hope is good for you.

    var arr : Array = [];
    var r   : int;

    for (var i: int = 0; i < 3; i++){
        r=rand(0,2);
        if(i == 1){
            if(arr[0] == r){
                i--;
                continue;
            }
            if(arr[0] == 0){
                if(r==1){
                    i--;
                    continue;
                }
            }
        }else if(i==2){
            if(arr[0] == r || arr[1] == r){
                i--;
                continue;
            }
        }
        arr[i] = r;
    }

    trace(arr);

    for(var i=0;i<3;i++){
        mcCor = new CorDaCarta();
        mcCor.x = larguraTrio + (mcCor.width + 5) * (i % 3);
        mcCor.y = alturaTrio + (mcCor.height + 5) * (Math.floor(i / 3));
        mcCor.gotoAndStop(arr[i]);  
        mcCor.buttonMode = true;
        mcCor.addEventListener(MouseEvent.CLICK, cliquetrio);
        mcExplic.addChild(mcCor);
        trio.push(mcCor);
    }


    function rand(min:int, max:int):int {
        return Math.round(Math.random() * (max - min) + min);
    }

try this...

tziuka
  • 545
  • 1
  • 6
  • 23