Questions about a random value between two numbers

Okay, basically I have three questions.

  1. Say I want to make a “speed” variable of type “float” to be a value between 10 and 15. What is the formatting for this?

  2. If the script with this variable were attached to a prefab, would every instance of that prefab have a different “speed” value?

  3. How would this randomization work? Like, what values would be selected? Would it just choose 10, 11, 12, 13, 14, and 15? Or would there be 10.5, 11, 11.5, 12, etc? Or would it go into even smaller numbers? Would there be a value of, say, 14.56783429?

Hopefully this is clear enough. Let me know if I need to clarify anything.

  1. var speed = Random.Range (10.0, 15.0);
  2. Yes.
  3. It chooses any float value between 10.0 and 15.0. If you want whole numbers, then use integers instead of floats (in which case the upper value is exclusive; look up Random.Range in the docs).

1 - Random.Range : Unity - Scripting API: Random.Range

2 - Yes, umless you make a static variable and just access it in every monovehabiour.

3 - Will be xx,xxxxx. If you want, you can round to decimals, example:

2 decimals Mathf.Round(yourFloat * 100f) / 100f; in this case value will be x.xx;

3 decimals Mathf.Round(yourFloat * 1000f) / 1000f; in this case value will be x.xxx;