- Home /
[C#] How do I randomly pick between 3 specific numbers?
Is it possible to CHOOSE a random number between 3 numbers I have set? I want to randomly choose between -3, 0, 3 but only those exact three numbers. I want my enemies to randomly spawn in one of the three lanes (x=-3/0/3). I am familiar with the Random.Range command but I am not sure I can force it to choose. Thanks in advance!
@pako's answer is a useful general method, however for your specific case it would probably be simpler to do Random.Range(-1, 2)*3;
this looks useful as well, I will keep that in $$anonymous$$d. I suppose Random.Range(-1,2)*3 will multiply the 3 possible results by 3, giving me -3, 0, 3. I wouldn't think of this, thanks Scribe!
Answer by pako · Dec 06, 2015 at 07:48 PM
Put the 3 numbers in an array. Then use Random.Range(0, 3) to select 0, 1, or 2 (the maximum '3' is excluded). Use the random result as the index of the array.
Your answer
Follow this Question
Related Questions
Random selection 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to stop MoveTowards teleporting 1 Answer
Random Children Array that changes Parent, not working right 0 Answers