Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:08+00:00 2026-05-26T23:20:08+00:00

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

  • 0

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

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:20:09+00:00Added an answer on May 26, 2026 at 11:20 pm

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

    • http://en.wikipedia.org/wiki/Fisher-Yates
    • http://jsfromhell.com/array/shuffle
    • Example: http://jsfiddle.net/Q9ZAZ/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to generate word document using assp.net/c#.net, please tell me
can any one please tell me how to make this code work in the
can anyone please tell me how to generate random numbers with no repeat example
Please tell me why my window doesn't render. Below is the javascript that i
Could you please tell what is the difference between a WCF client and a
I am new in android, Please tell me how we can generate the mouseclick
Can you please tell me which is the best tool that generate the pictorial
Can you please tell me if there is any open source library which generate
Hi can anyone please tell me why the following code is only returning the
When I execute this code in plain java and in android, I get different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.