- Home /
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?
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.
Sounds like an infinite loop. I cannot see where... yet...
How to fix is script and how to you finish loop answer me please
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
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.
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
Follow this Question
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