- Home /
loop until u find non duplicate
I have this code:
chknext = Random.Range(0, 6);
int checkmonspawn()
{
getrannum = Random.Range(0, 4);
if (getrannum == chcknext)
{
getrannum = Random.Range(0, 4);
}
return getrannum;
if getrannum and chcknext is the same, getrannum will get another random number. Problem is sometimes he gets the same number What is the efficient way to do this?
You could change if to while. It's a quick and dirty solution but not a good one if it needs to be called a lot.
Is there any certain reason why you are calling Random.range two-three times? What are you trying to achieve with using two random integers? Also assu$$anonymous$$g you declared chknext and getrannum as ints?
Answer by AurimasBlazulionis · Sep 13, 2016 at 03:58 AM
You can easily change if (getrannum == chcknext)
to while (getrannum == chcknext)
. This will repeat the steps until not the same number will get generated. This is completely safe, because I can not even imagine 10 times in a row to get 2 generated. And we are talking about this being run on modern computers, which can do millions of computations a second.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
"Nothinge Selected" 1 Answer
Particle System: Color over Lifetime - How to control alpha ONLY 2 Answers
How to using remote Bluetooth button to tap on screen 0 Answers