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.
1 Answer
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);
}
}
1 Comment
Wollie
that's good for me, thanks! Are there perhaps alternatives?
Randomclass