• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 20, 2014 at 03:07 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by tuhin_09 · May 27, 2014 at 02:09 PM · arrayrandomalgorithmnumber

How to generate unique random number

hi , i am new in unity , but i work at IOS cocos2d and sprite-kit. i made unique random number at cocos2d the code are given below , but i just cant make that thing convert properly at unity

here is the code at objective c

 -(void)CreateRandomCircle{
     int Range = 20;
     int random;
     
     int checker[Range];
     for(int i = 0 ; i < Range ; i++)
     {
         checker[i] = 0;
     }
     for(int i = 0 ; i < Range ; i++)
         
     {
         random = arc4random() % Range;
         
         while(checker[random] == 1)
             
         {
             random = arc4random() % Range;
         }
         checker[random] = 1;
         
         NumArray2 [i]=random;
     }
 }


but i tried to make it in unity here is my try

 private int randomm;
 
     private int[] checker = new int[];
     private int[] UnicNumArray = new int[20];

 
     void CreateRandomCircle()
     {
         checker = new int[20];
         for (int i = 0; i <20 ; i++)
         {
                 checker [i] = 0;
             randomm = Random.Range (0, 20);
             print(randomm);
 
             while (checker[randomm] == 1) 
             {
                 randomm = Random.Range (0, 3);
             }
             checker [randomm] = 1;
             UnicNumArray [i] = randomm;
         }
     }


please this will be a big time help .

again all i want is 20 unique random number at one single array

Comment
$$anonymous$$

People who like this

1 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vptb · May 27, 2014 at 02:30 PM 0
Share

So you want an array of 20 elements, with unique random numbers in there? And those numbers range from 0 to 19?

avatar image vvkkumar06 · May 27, 2014 at 02:34 PM 0
Share

You have to generate random numbers and save in an array.. and compare each time with previously generated random number using a loop and if-else condition.

avatar image PouletFrit · May 27, 2014 at 02:44 PM 0
Share

20 unique random number between 0-20? integer or floating point number?

avatar image incorrect · May 27, 2014 at 03:00 PM 0
Share

You'd better generate straight array of numbers from 1..19 and then shuffle it by moving randomly picked numbers from it in new array. This way worked nice when i used dynamic arrays in JS and picked using Array.length*Math.Random() as index.

avatar image meat5000 ♦ · Nov 20, 2014 at 03:06 AM 0
Share

2 searchable terms for use in answering this question:

ShuffleBag

Random Seed

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Anxo · May 27, 2014 at 02:56 PM

Something like this? I have not tested it but this should give you a list of int from 0 to 19 in a random order. If I understood your question correctly.

 using System.Collections.Generic

 private int maxNum = 20;
 private List <int> randomList;
 
 
 public void GenerateRandomList (){
     for(int i = 0; i < maxNum; i++){
        int numToAdd = Random.Range(0,maxNum);
        while(!randomList.contains(numToAdd)){
           numToAdd = Random.Range(0,maxNum);
        }
        randomList.Add(numToAdd);
 
    }
    //Done. 
 }
Comment
instruct9r
$$anonymous$$
tkamruzzaman
newleon
computas
Hims
edrogersiv
Thanaor
OfficeThrashing
xinyunzhong
oleksandr_kov

People who like this

11 Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anxo · May 27, 2014 at 03:06 PM 0
Share

Oh do not forget

 void Start(){
    randomList = new List<int> ();
 }


And a cleaner version might actually be something like this instead.

 private int maxNumbers = 20;
 private List<int> uniqueNumbers;
 private List<int> finishedList;
 
 void Start(){
    uniqueNumber = new List<int>();
    finishedList = new List<int>();
 }
 
 public void GenerateRandomList(){
    for(int i = 0; i < maxNumbers; i++){
       uniqueNumbers.Add(i);
    }
    for(int i = 0; i< maxNumbers; i ++){
      int ranNum = uniqueNumbers[Random.Range(0,uniqueNumbers.Count)];
      finishedNumbers.Add(ranNum);
      uniqueNumbers.Remove(ranNum)
    } 
    //Done.
 }

I think this is cleaner b/c it leaves less things to chance.

avatar image tuhin_09 · May 27, 2014 at 05:01 PM 0
Share

thank you.........its works........thnx a lot

avatar image TKS_Keeper · May 27, 2014 at 05:30 PM 0
Share

Remember to accept the answer if it worked.

avatar image nur farazi · May 28, 2014 at 05:03 PM 0
Share

i was having the same problem , thank you

avatar image BlasterJoker · Nov 20, 2014 at 03:04 AM 0
Share

for the first solution, line 10 is not suppose to have the "!" symbol right? because we want to gen a new number if it DOES contain it.

Show more comments

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random texture changer (problem with array) 2 Answers

random number generator problem 1 Answer

How to prevent picking the same combination in array? 1 Answer

How to randomize my maze generation algorithm(DFS & stacks) 1 Answer

RNG output to a variable + instantiating a prefab based on said variable? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges