I have a random number class (and I've experimented with making it static, but then for some reason I can't generate a random number if it's static because it says that the random class is not defined within the scope).
public class GlobalRandom
{
public int generateRandom()
{
Random rnd = new Random();
int randomNum = rnd.Next(0, 10);
return randomNum;
}
}
Then I create an instance of it in another class and method where I want the random number.
GlobalRandom globalrandom = new GlobalRandom();
int randomNumber = globalrandom.generateRandom();
However, I want the SAME random number to be seen across the whole program and able to be used by any class. I can do this with other variables, but not with random because if you try and create a static method it won't let you use the random class. I'm at a loss at how to do this.
Background info: This is for an ASP.NET web app, so helping me with a console-style system won't be relevant, since I'm not creating instances of the other classes, but rather returning a JSON object for AJAX. The object I'm returning is determined by the random number, then I send a value back to the controller that has to match up with a value that was picked by the same random number, otherwise it just won't work!
Aucun commentaire:
Enregistrer un commentaire