Random int Value

I'm making a game (aren't we all? ;) ) and I need a random value. I know how to do this with `Random.Range`, but for my game I need an `int` type, not `float`. Is there a way to round a `float` value to an `int` this in javascript with a rounding function or even just using the `Random` class? Thanks in advance!

3 Answers

3

Hi, its in the docs, but its important to mention the exclusive and inclusive difference. If using floats, the min and max value are inclusive, if using integers, the max value is exclusive.

Examples:
When defining Random.Range(1.0f, 3.0f) we will get results from 1.0 to 3.0
When defining Random.Range(1,3) we will get results from 1 to 2

Regards

Random.Range returns an int if you use it with integers. Random.Range in the docs

@Eric5h5: Thanks for the quick answer, I apparently did not notice that when I looked up Random.Range in the the scripting manual...

Also look up Mathf - while you don't need it now it has a whole bunch of features including rounding that may be useful to you later on.

you allways need to add one to he top number
just the way unity works