0

i have this function

//Random Number
    //-------------
    static int RandomNum(int LoLim, int HiLim)  
    {
        Random random = new Random();
        return random.Next(LoLim,HiLim); 
    }

which is used like 3 or 4 times in this lottery thing. it works in Debugging but when i just run it regularly it gives me the same number.

i've read some stuff about how i'm supposed to initiate the random outside the main thing, but i'm not sure how to do that with a function.

Muffinator
  • 143
  • 2
  • 12
  • What are the values you use for LoLim and HiLim? – Nipuna Apr 18 '15 at 13:56
  • 1
    http://stackoverflow.com/q/767999/447156 ? – Soner Gönül Apr 18 '15 at 13:56
  • 5
    Sure, creating a new instance of Random whenever you need another random number does not work. It is seeded with the current time, the clock changes no more often than once every 16 milliseconds. Works when you debug, you slow down your program enough, not when you run at full speed. Move the variable outside of the method so you create the object only once. – Hans Passant Apr 18 '15 at 13:57
  • @Nipuna i use different values when i call to the function in different parts of the program – Muffinator Apr 18 '15 at 13:58
  • @HansPassant i don't know how to do that though. if i do the `Random random = new Random();` it won't recognize `random` in the function – Muffinator Apr 18 '15 at 14:02
  • Put *static* in front of it. – Hans Passant Apr 18 '15 at 14:04

0 Answers0