Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random number between 1 and 3 / including 1 decimal place

I can generate a random number between 1 and 3 fairly easy.

float x = Random.Range(1, 3);

But I am trying to generate a random number between 1 and 3, including 1 decimal place. i.e 1.0, 1.1, 1.2 - 2.8, 2.9, 3.0 Any help appreciate. I am finding no easy function to do this.

Note - I am using .cs script in Unity

like image 933
user3609198 Avatar asked Dec 02 '22 19:12

user3609198


1 Answers

You can multiply the result like this:

float result = rnd.Next(10, 31) * .1f;

This will result in a range from 1.0 to 3.0, stepping by .1.

like image 115
Nathan A Avatar answered Dec 13 '22 04:12

Nathan A