Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by Matt 15 · Nov 17, 2010 at 09:34 PM · arraylistrandomcheckduplicate

unique list of 10 random numbers

Hey there unity Answers!

I'm trying to get 10 random numbers from 0 - 120, then store those numbers into a array (or list) only trick is, i keep getting duplicates. is there a good way to get the random number (random.range(0,120) then add it to an array, ONLY if it isn't already in there?

thanks in advance!

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Eric5h5 · Nov 17, 2010 at 09:53 PM

You could have a list of all numbers from 0-120, then remove the ones you've already picked for the array:

function Start () {
    var numbers = new List.<int>(121);
    for (i = 0; i < 121; i++) {
        numbers.Add(i);
    }
    var randomNumbers = new int[10];
    for (i = 0; i < randomNumbers.Length; i++) {
        var thisNumber = Random.Range(0, numbers.Count);
        randomNumbers[i] = numbers[thisNumber];
        numbers.RemoveAt(thisNumber);
    }
}
Comment
Add comment · Show 10 · 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 Matt 15 · Nov 18, 2010 at 02:05 AM 0
Share

is there a way to just add the number to a new list, if it isn't already in that list?

avatar image Eric5h5 · Nov 18, 2010 at 02:42 AM 0
Share

Didn't really understand that...is this something new? The code I wrote answers the original question.

avatar image Matt 15 · Nov 18, 2010 at 02:52 AM 0
Share

sorry bout that. unless I'm reading the code wrong, it seems like it's going to remove the random numbers (thisNumber) from the array of numbers from 0-120. I was wondering if you can check if the random number (thisNumber) has been picked, and if not add it to a list of the random numbers it picked, until it reaches 10 unique ones.

avatar image Matt 15 · Nov 18, 2010 at 03:51 AM 0
Share

actually, sorry again, I read the code wrong, got it now. :/ THAN$$anonymous$$S!

avatar image Eric5h5 · Sep 18, 2014 at 07:14 AM 1
Share

@vfxjex It doesn't take "a lot of processing", it's utterly trivial. It's just a loop using List.Add, and hardly takes more time than allocating the list itself (at least with a size that small).

Show more comments
avatar image
1

Answer by IJM · Nov 17, 2010 at 09:54 PM

Every time you are adding a new random number to your list you sould go thro all numbers in the list to see whether that number alredy exists in the list. If it exists in the list, you ask for another one, if not you add it to the list.

Comment
Add comment · 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
1

Answer by poncho · Jan 06, 2011 at 10:58 PM

this is an other way to doit, my code suggestion would be this

void Start()
{
   //having an int Array of 10 members
   int [] numbers = new int[10];
   //using a string variable to keep record of the used numbers
   string usedNumbers ="-";
   //cycle to get 10 random numbers
   for (int i = 0 ; i<10; i++)
   {
      //get a random number between 0 - 120, 0 and 120 included on the random numbers
      int randomNumber = Random.Range(0,121);
      //Checking if the random number you get is unique, if you already have it in the string means that this random number is repeated, the "-X-" is to make the difference between the numbers, like -1- from -11-, if you dont have the "-X-" the 1 is in 21 and would be a "repeated" number
      while(usedNumbers.Contains("-"+randomNumber.ToString()+"-"))
      {
         //if a number is repeated, then get a new random number
         randomNumber = Random.Range(0,121);
      }
      usedNumbers+= randomNumber.ToString()+"-";
      numbers[i] = randomNumber;
   }
}

with this code you will never have 2 same numbers

hope the explanation of the code helps you

Comment
Add comment · Show 3 · 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 Eric5h5 · Jan 06, 2011 at 11:20 PM 0
Share

Your understanding of my code is completely wrong. It does exactly what the question asked, in a reasonably efficient way, and has no chance at all of getting the same numbers. Your code is brute-force, plus it has highly inefficient string conversions, comparisons, and concatenations. Also it doesn't scale well...for example it will perform quite badly if you wanted a unique list of 110 out of 120 random numbers, whereas $$anonymous$$e would still be just as fast, aside from looping 110 times ins$$anonymous$$d of 10.

avatar image poncho · Jan 07, 2011 at 12:15 AM 0
Share

yeah a few secs after posting i read again and it was correct XD sorry dude, thats why i changed my answer, just give this answer for another way to do it, about the performance, is quite stable, cuz the memory just need 1 byte for each char in the string, not needed to make the whole array, anyway i just gave an other way of thinking and provided how the logic was developed, for own understanding of the code, this means that he can learn from the code, not only copy paste sorry for the misunderstanding

avatar image Eric5h5 · Jan 07, 2011 at 01:12 AM 0
Share

No, chars are more than one byte since they're Unicode. Also, every time you use "+" to concatenate a string, it has to allocate another complete string, which ends up being extremely wasteful when you do it repeatedly. The garbage collector gets it back, but it's still a waste of resources and CPU time. Ins$$anonymous$$d of abusing strings that like that, it would be a lot better to simply check the actual contents of the array, but that technique still has the problem of not scaling well like I mentioned. It only works well if you have a small selection of random numbers out of a much larger set.

avatar image
0

Answer by steddyman · Mar 09, 2013 at 12:17 AM

Recommend you take a look at Unity Random on the asset store. It has shuffle bags which would fix this. I use it for a similar scenario.

Comment
Add comment · Show 1 · 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 vertexx · Mar 25, 2021 at 12:23 AM 0
Share

Unity Random? Link please. Sounds interesting. I found that poster "Eric5h5" did things perfectly but Unity Random might be worth a look for folk interested in this sort of thing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Pick between two floats 2 Answers

Random select from array and spawn 1 Answer

Need some help with Array searching logic 1 Answer

Selection list from Array Unity - Random - GameObjects array 1 Answer

A node in a childnode? 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