I can generate a random number between 1 and 3 quite easily.
float x = Random.Range(1, 3);
But I'm trying to create a random number between 1 and 3, including 1 decimal place. that is, 1.0, 1.1, 1.2-2.8, 2.9, 3.0 Any help appreciated. I do not find an easy function for this.
Note. I am using .cs script in Unity
You can multiply the result as follows:
float result = rnd.Next(10, 31) * .1f;
This will lead to a range from 1.0to 3.0, step by step .1.
1.0
3.0
.1
It seems solid.
class Program { static void Main(string[] args) { float fMyNumber = 0; Random rnd = new Random(Guid.NewGuid().GetHashCode()); for (int j = 0; j < 20; j++) { fMyNumber = rnd.Next(1, 4); fMyNumber += ((float)rnd.Next(10)) / 10; Console.WriteLine(fMyNumber.ToString("0.0")); } Console.ReadLine(); } }
:
Random.Range(1.0, 3.0);
, , Random.Range(), float, int. , , , f , . Random.Range(1.0f, 3.0f).
Random.Range , 3.1f 3.0f.
, , , - , .
, Unity, System.Random.
System.Random rnd = new System.Random();
Unity , .
Source: https://habr.com/ru/post/1539472/More articles:AngularJS: how to log out when cookie expires - javascriptiframe with event pointers: none; but with a div inside it with an event pointer: auto; - html5rails 4: base class type of polymorphic set instead of inherited - polymorphismмогу ли я сделать файл .jar совместимым со старыми версиями java - javanumpy - как массивы внешних соединений - pythonLocalDB and Entity Framework 6 - Security - entity-framework-6gfortran does not work on Mac OS X 10.9 (Mavericks) - gfortranchange background color once every few seconds - background-colorgfortran does not work on Mac OS X 10.9 - gfortranavoid duplicate constructor in specialized specialization - c ++All Articles