Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by switchup621 · Mar 07, 2014 at 08:39 PM · c#randomrandom.range

Non-repeating random number generator crashing unity

Hi everyone,

I know there are some threads about non-repeating random number generators, but I wanted to write my own. However, my code keeps crashing Unity and I cannot figure out why.

     private int[] used_list = new int[10];
     public int trial = 1;
     private int intRand;
     private bool success_flag = false;
     private int position;
     private GameController gameController;
 
 for (int i = 1; i < 5; i++) {
         while (success_flag == false)
         {
             intRand = Random.Range(1,4);
             success_flag = true;
             
             for (int j = 1; j < trial; j++)
             {
                 if (intRand == used_list[j]) { success_flag = false; }
             }
         }
         
         used_list[trial] = intRand;
         
         success_flag = false;
         
         
         if (used_list [trial] == 1) {
             position = 0;
             gameController.position = "AB";
         } else if (used_list [trial] == 2) {
             position = 90;
             gameController.position = "BC";
         } else if (used_list [trial] == 3) {
             position = 180;
             gameController.position = "CD";
         } else if (used_list [trial] == 4) {
             position = -90;
             gameController.position = "AD";
         }
         
         Debug.Log ("The wall " + gameController.position);
         Debug.Log ("The position " + position);
             Debug.Log ("Should be a random number " + used_list[trial]);
             Debug.Log("The trial " + trial);
 
             trial += 1;
 
     }

 

So what this script does is pick a random number and check if the number has already been used in the array. If it has, the success_flag is set to false and the loop starts again and picks a new random number. It does this until a number that hasn't been used is picked. That number then gets saved in position "trial" for the array used_list.

The current random number should then be shown by calling used_list[trial]. Unfortunately, this crashes the program. Anyone know why?

Comment
Add comment · Show 4
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 Dblfstr · Mar 07, 2014 at 08:55 PM 0
Share

Are you getting any errors?

avatar image switchup621 · Mar 07, 2014 at 08:58 PM 0
Share

Nope, my scene just doesn't run when I hit play and then I need to use Ctrl+alt+Delete to close unity. If I get rid of the trial += 1 part at the end, it runs but then repeats numbers.

avatar image Dblfstr · Mar 07, 2014 at 08:59 PM 1
Share

Sounds like an infinite loop. I cannot see where... yet...

avatar image hern · Apr 04, 2014 at 03:24 AM 0
Share

How to fix is script and how to you finish loop answer me please

1 Reply

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

Answer by maxxa05 · Mar 07, 2014 at 09:07 PM

Random.Range with int, the second parameter is exclusive, so it never gives 4. use Random.Range(1,5), and you won't get an infinite loop

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 switchup621 · Mar 07, 2014 at 09:12 PM 0
Share

Ah, that was it! Thank you!

avatar image ArkaneX · Mar 07, 2014 at 09:33 PM 2
Share

This is of course a valid answer, but if you plan to use this code to generate more random numbers, e.g. in range 1 - 100, then you need to change it. Currently, you're wasting a small number of Random calls, but for 1 - 100 range, you'll waste about 10000 of those. It could have a real impact on performance.

avatar image maxxa05 · Mar 07, 2014 at 09:39 PM 1
Share
  • to ArkaneX, this is not an optimized algorithm, you'll need to change it if you want a lot of iterations. The time this algorithm will take will grow exponentially.

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

24 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

Related Questions

Need help with generating random Multiplier,Need help with generating random multipliers 0 Answers

Distribute terrain in zones 3 Answers

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

How can I randomly create a number and then delete it to stop repeating 3 Answers

Semi-Random Or Engine 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