Domanda

please tell me how to generate 5 different non repeatable random number between 1,20 in javascript for unity3d

this the code i have written.. this works 1st 3 times but crashes the last time. please tell me how i can improvise this i have taken numbers in an array to print them on screen n n checked for their repeatability. this works ly 3 times.. for 4th time unity crashes. i know the reason dunno how solve it.. i think it is because it is not able to detect the remaining numbers of the array. Please tell me where i have gone wrong.. i'm new to unity.

var temp : int = 0;
var array = new Array();
var count : int = 0;
var n : int = 0;
var b : int = 0;
var num = new Array();
var num1 : int = 0;
var num2 : int = 0;
var num3 : int = 0;
var num4 : int = 0;
var num5 : int = 0;

var number1 : GUIText;
var number2 : GUIText;
var number3 : GUIText;
var number4 : GUIText;
var number5 : GUIText;

function Start () {

if((temp == 0) || (temp ==20))
{
    //for(a=1;a<=20;a++)
    //{ 
        temp=0;
        array[1]=1;
        array[2]=2;
        array[3]=3;
        array[4]=4;
        array[5]=5;
        array[6]=6;
        array[7]=7;
        array[8]=8;
        array[9]=9;
        array[10]=10;
        array[11]=11;
        array[12]=12;
        array[13]=13;
        array[14]=14;
        array[15]=15;
        array[16]=16;
        array[17]=17;
        array[18]=18;
        array[19]=19;
        array[20]=20;
        //array[1]=a;

    //}

}

}

function OnGUI ()
{

if (GUI.Button (Rect (100,200,100,100), "Click Me"))
{   
    count=0;
    //print(count);
    genrandom() ;
    for(a=1;a<=6;a++)
    {
        //print("A is"+a);
        genrandom() ;
        temp++;
        count=count+1;
        //print("Count is :"+count);
        if(count>=6){count = 0;}
    }
  }
  }

function genrandom() 
{
//temp++;
//print(temp);
if(count<=5)
{   
    n = Random.Range(1,20);
    //print("N is"+n);

    if(array[n]!=0)
    {

        if(count==1) 
        {
        print("Count is 1");
        num1 = array[n];
        array[n]=0;
        number1.text = ""+num1;
        //count++;
        }
        if(count==2) 
        {
        print("Count is 2");
        num2 = array[n];
        array[n]=0;
        number2.text = ""+num2;
        //count++;
        }
        if(count==3) 
        {
        print("Count is 3");
        num3 = array[n];
        array[n]=0;
        number3.text = ""+num3;
        //count++;
        }
        if(count==4)    
        {
        print("Count is 4");
        num4 = array[n];
        array[n]=0;
        number4.text = ""+num4;
        //count++;
        }
        if(count==5)
        {
        print("Inside count= 5");
        num5 = array[n];
        array[n]=0;
        number5.text = ""+num5;
        //count++;
        }   
    }
    else
    {
        genrandom();
    }

}
}
È stato utile?

Soluzione

One way to go about this would be to create the array of possible values, shuffle it, and look at the first five elements:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top