- Home /
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!
Answer by Eric5h5 · Sep 26, 2010 at 07:34 AM
Random.Range returns an int if you use it with integers. Random.Range in the docs
Doh, your answer hadn't appeared when I went to post. Deleting $$anonymous$$e.
Also look up $$anonymous$$athf - while you don't need it now it has a whole bunch of features including rounding that may be useful to you later on.
Answer by khross · Mar 19, 2015 at 10:52 PM
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
Answer by roymeredith · Jul 13, 2019 at 03:32 PM
you allways need to add one to he top number just the way unity works
That's not the way Unity works, that's how almost all random number generators for integer numbers work (processing, .NET / C#, Java, javascript, python, golang, Delphi, lisp, octave).
And you don't always have to add 1. One of the most common usage is this:
SomeType[] someArray;
someArray[Random.Range(0, someArray.Length)]
Your answer
![](https://koobas.hobune.stream/wayback/20220613073939im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script not working (Java script) 2 Answers
Code to randomly generate a mesh? 0 Answers
Random spawn timer 1 Answer
Why is my random sound java script not working? 2 Answers
GUI Text to GUI Label Script? 1 Answer