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 /
avatar image
0
Question by DragorKG · Apr 16, 2019 at 05:27 PM · freezingwhile loop

Unity freezing on play in "Rrandom number order" function

Hello, I am making survey app in Unity. But have problem with function that are generating numbers from 0 to l-1 in random order, while storing them into list questionOrder.

Problem occurs (Unity freezing on play) when I put one specific condition in if statement bellow.

     int l = *someList*.Count; // Length I want my new integer list (questionOrder) to be
     int i = 0; // current length of *questionOrder* list
     while (i < l)
     {
         int r = Random.Range(0, l - 1);
         int k = 0;        
  
         while (i > 0 && k < questionOrder.Count && r != questionOrder[k])
         {
             k++;
         }

         if (i == 0 || k == questionOrder.Count) // *k < questionOrder.Count* is problematic
         {
             questionOrder.Add(r);
             i++;
         }
     }

First I am generating random number r, then with 'nested while' loop checking if generated number exists, if not 'nested while' will end because k == questionOrder.Count (r is compared with every list member, that means r is unique). Then, in IF statement I am checking what was the reason 'nested while' ended (if k == questionOrder.Count then r is unique and will be added to questionOrder list).

If i remove k == questionOrder.Count and set i == 0 to i >= 0 (r does not need to be unique) it works fine.

Note: i > 0 in 'nested while' and i==0 in if are just to work when the questionOrder list is empty.

Comment
Add comment · Show 2
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 mchts · Apr 17, 2019 at 11:53 AM 1
Share

If what you want is to randomize question order, you can use Fisher–Yates shuffle Algorithm to shuffle an array.

 //this is where you have an array as lengt of l 
 int l = *someList*.Count;
 int[] arr = new int[l];
 for(int i = 0; i < l; i++){
     arr[i] = i;
 } 
 arr = randomize(arr, l); //randomize
 
 //Fisher–Yates shuffle Algorithm
 static int[] randomize(int []arr, int n) 
 { 
     Random r = new Random(); 
     for (int i = n - 1; i > 0; i--)  
     { 
         int j = r.Next(0, i+1); 
           
         int temp = arr[i]; 
         arr[i] = arr[j]; 
         arr[j] = temp; 
     } 
     return arr;
 }

avatar image DragorKG mchts · Apr 26, 2019 at 06:18 PM 0
Share

Hi, thank you for replay!

$$anonymous$$y friend also gave me advice to use this algorithm, so I will definitely use it.

But I still do not understand why my script causes Unity freezing. I know it is very unoptimized, but I tried my script in c# console application and it works fine.

Do you have any idea why it can be problematic for Unity?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 27, 2019 at 02:54 AM

With this statement:

 int r = Random.Range(0, l - 1);

you can not generate "l" different random numbers since the number count you specify is "l-1". So you will never hit your desired count. This is because Random.Range with integer parameters does exclude the upper limit. So Random.Range(0, 3) can return 3 different numbers 0, 1 or 2 but never 3.


Since your break condition is i >= l it will never be reached since the number range you draw from is one less than your desired target amount- So there's no sequence of unique numbers which can do this.


Your line should have looked like this:

 int r = Random.Range(0, l);


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

103 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 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

Character freezes when I change camera 3 Answers

Game Tab Freezes in Play Mode, MacOS Sierra, Unity 5.4.1f1 1 Answer

My game keeps freezing 1 Answer

instantiate object infinitely 0 Answers

While "loop" with 2conditions, ends before it reach the conditions oO ? 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