I use .NET Random class for generating samples in the range <0, 1.0). Note the upper bound is not included in this range. I then cast the result from double to float. Can this casting cause in rare cases rounding to 1.0? I'm not an expert in IEEE floating-point mechanics.
I tested this for you with the following code: Code (CSharp): double d = 0.9999999999999d; Debug.Log("Double value: " + d); float f = (float)d; Debug.Log("Float Value: " + f); double d2 = double.MinValue; Debug.Log("Double value 2: " + d2); float f2 = (float)d2; Debug.Log("Float Value 2: " + f2); Getting the following results: > Double value: 0,9999999999999 > Float Value: 1 > Double value 2: -1,79769313486232E+308 > Float Value 2: -infinity So apparantly yes, values can get rounded to 1 or infinity, even if they previously were not. Which makes sense, since we are losing precision. If necessary, you should check the value and reset it to <1 manually when the value is 1.
I use it in random level generation. I got sometimes a crash because I then casted to int and indexed an array using Length instead of Length-1.
Maybe use int instead as random? Is more stable. Or uint, if you don't want negatives. Then you can use seeds. However int on its own is very long value mind. Do you realm need more than that? With float, passing seed is unreliable. What you can also do, is generate int,uint,long etc. and convert to float. Why do you need double precision, if you cast from float, to double float? Your results indeed maybe unpredictable. Do you do some additional math on new variable?
It's probably not that they need double precision. But rather that they're using System.Random which has 'Next' and 'NextDouble' which return int and double respectively. And they're going with NextDouble since it's a float rather than an int, and comes in the 0->0.99999 range that they expect rather than 0->int.Max (or the max passed in). @OP, your options are: use 'Next' with a max value of int that fits in float (16777216 ?), convert that int to float, and then divide that by the max value use 'NextDouble', cast to float, and use Clamp (or Math.Max/Min) to clamp it into 0->0.99999f.