Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by tippi-co · Jul 21, 2016 at 03:40 PM · listrandomrandom.rangesortingsort

Generate unique random numbers and sort them.

I'm trying to get 4 unique random numbers out of 1-50. For example, if I get (45,20,1,18), sort them to be (1,18,20,45).

However, my script generates 18 numbers and include the same numbers.

e.g. result{22.29.38.1.13.27.17.37.39.9.17.21.37.39.40.27.29.43} This time, double 17 and 37.

Could you help me to check my script? Why doesn't it work like I expect ?

I'd really appreciate your help!! Thank you.

  void Start(){

      List<int> fulNumList = new List<int>();
      List<int> pickedNumber = new List<int>();

 
      for (int i = 1; i < 50; i++) {
              fulNumList.Add(i);
      }
      //now all possible numbers are in fulNumList
 
      for (int i = 0; i < 3; i++) {
 
          int indexNum = Random.Range(0,fulNumList.Count);
          //get a random index number form 0-49
 
          int randomNum = fulNumList [indexNum];
 
          pickedNumber.Add(randomNum);
          //store randomNumber into pikedNumber list
              
          fulNumList.RemoveAt(indexNum);
          //remove the number that stored in pickedNumber from fulNumList for avoiding repeat numbers 
      }
 
      pickedNumber.Sort ();
 
      foreach (int result in pickedNumber){
          Debug.Log (result);
      }
 
  }
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

3 Replies

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

Answer by · Jul 21, 2016 at 04:20 PM

I changed this line "for (int i = 0; i < 3; i++)" to "for (int i = 0; i

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 tippi-co · Jul 22, 2016 at 04:57 AM 0
Share

Thank you for the help! Oh yes! (>=) is right here for 4 numbers.

And...I'm really ashamed to confess what my big problem was. I attached the scrip to 6 of objects, so that I got 6 times of 3 numbers! (18 numbers).

avatar image
0

Answer by tippi-co · Jul 22, 2016 at 04:59 AM

Thank you for the help! You have tried my script ! Thank you for taking your time!

Yes, I did mistake that part. I changed it now and I got 4 numbers.

And as I mentioned it on comment for _Nyro, I was really stupid that I attached the script to 6 objects, so that I got 6 times of the result!

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
0

Answer by MrJamieMcC · Jul 31, 2017 at 12:51 PM

Is it ok if i use this script?

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

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

67 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How I Sort a List at Start?,Sorting a List on Start 0 Answers

Sort list by distance AND by being active 3 Answers

What range of values need to be inserted if we need to pull a random item from the list? 0 Answers

How do I get random answers? 2 Answers

Random.Range code problem or problem with Unity? 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