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
0
Question by tippi-co · Jul 17, 2016 at 03:01 PM · random.rangealignnumbers

Align different random numbers smaller to larger

Hi. I want to get four different random numbers that range 1 to 50, and align them smaller to bigger.

For example, if I get (45,20,1,18), align them to be (1,18,20,45).

My script is stacked at "random.range(1,51)". Could you please give me some advice?

Thank you 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

2 Replies

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

Answer by gjf · Jul 16, 2016 at 05:55 PM

that's called sorting. put them in a List and then use listName.Sort();

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 21, 2016 at 03:46 PM 0
Share

Thank you for your help.

Sorting is what I was looking for! But, unfortunately my script doesn't work. I repost this with my script in the bellow link.

I'd really appreciate if you could check it! Thank you.

http://answers.unity3d.com/questions/1219159/sort-unique-random-numbers.html

avatar image
0

Answer by tippi-co · Jul 18, 2016 at 09:51 AM

Thank you for the help! I'm trying it, but it's something wrong . I get 18 of random, but repeatable numbers and not sorted.

result{22.29.38.1.13.27.17.37.39.9.17.21.37.39.40.27.29.43}

This time,two 17 and 37.

Could you help me to check my script?

         List<int> fulNumList = new List<int>();
     List<int> pickedNumber = new List<int>();
 
     void Start(){
 
         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 · 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I produce code that will return a number between 1-6, then display the number. 1 Answer

Random.Range not working? 2 Answers

Generate Random Numbers a Distance Apart From Each Other 1 Answer

How do I get random answers? 2 Answers

Problem with random Range, returns always same number 3 Answers


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