- Home /
Random numbers please help
How can I set a value to an int var by random?
I mean if I have a variable called "numbercount" and I want to give it a value from 1-5 for exapmle, how can I do it?
Answer by landon912 · Nov 12, 2012 at 07:30 PM
Random.Range(1,5)
returns a random int from ONE to FOUR
DON'T FORGET, with integers it is ONE LESS THAN THE LAST ONE.
Thus, Random.Range(0,10) will give you "ten different" random numbers (that is, 0 through 9 inclusive).
To "roll a dice" it would be Random.Range(1,7).
it's ALWAYS one less than the end number you supply in the brackets. (the reason for this is to do with working with arrays. If you don't understand this yet, don't worry about it. Just don't forget it's one less than the end number.)
Just to clarify, that means numbers 1, 2, 3 and 4 could possibly be returned. 5 will never be.
Thanks for fixing my answer. Also solves another problem. Thanks