Forum C# random numbers on serverside

random numbers on serverside

Postby rejoin » August 25th, 2011, 5:37 pm

i need some help with generating random numbers serverside:
i use this, but it only gives me the 1 same random number again and again prolly because its always the same 1st number from the random table created. However if i try to take the random object out of the function and initialize it before i cant acces it afterwards because of static beeing prevented... im really new to c sharp =P help please

public static int getr(int min, int max)
{
Random r = new Random();
return r.Next(min, max);
}
Last edited by rejoin on August 25th, 2011, 11:36 pm, edited 1 time in total.
rejoin
 
Posts: 2
Joined: August 21st, 2011, 1:48 am

Re: rnadom numbers on serverside

Postby Mrx3D » August 25th, 2011, 9:09 pm

You don't have to create the random generator everytime you need a new number, that is also why you get the same number as result of you function. do this instead :

Random r;

public void init_random()
{
r = new Random();
}

public int getr(int min, int max)
{
return r.Next(min, max);
}

and to use it you only have to call init_random once:

...
init_random();
int value_a = getr(0,100);
int value_b = getr(0,100);
...
Mrx3D
Paid Member
 
Posts: 31
Joined: February 4th, 2010, 8:32 pm

Re: random numbers on serverside

Postby rejoin » September 6th, 2011, 10:45 am

thanx for the help =) but i got the same problem =P it doesnt even start because:

Feldinitialiser can't point to a none static Field

i got it working now =P i guess i just can't use random numbers as standard values =P
rejoin
 
Posts: 2
Joined: August 21st, 2011, 1:48 am


Return to C#