I am trying to have the field "weight" in a class "apple" to be filled with a random weight - let's say between 80g an 120g. I would like to use the parameterless constructor of apple. Which would be the recommended way to do this.
I recommend to use static Random instance and re-use it on every constructor call like this:
class Apple
{
private static Random rng = new Random();
public int weight;
public Apple()
{
weight = rng.Next(80, 121);
}
}
Random
class – Rahul Dec 12 '18 at 12:42