Forum Multiplayer Serverside Random Number Generation

Discussion and help relating to the PlayerIO Multiplayer API.

Serverside Random Number Generation

Postby JKillian » February 16th, 2013, 7:02 pm

Could you guys add some sort of easy to use global random number generator to the Player.IO library? Essentially without being able to have static fields, you have to either pass around or create a new instance of Random each time you want to generate random numbers. The first isn't very practical and the second can lead to hard to catch bugs and weird behavior when you end up getting Random instances that are seeded the same and thus generate the same results.

Maybe I'm just missing an easy solution or looking at something wrong, but it seems like this could be a fairly common problem that many people would have.
JKillian
 
Posts: 3
Joined: March 21st, 2012, 1:32 am

Re: Serverside Random Number Generation

Postby Pawel Wozniak » February 16th, 2013, 7:27 pm

i solved it this way:

public int getRandomValue(int min, int max)
{
DateTime now = DateTime.Now;
int diff = max - min;
if (diff == 0) return 0;
return min + ((now.Millisecond+random.Next(diff)) % diff);
}
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Serverside Random Number Generation

Postby Henrik » February 16th, 2013, 10:02 pm

Why not just put an instance on the Game and create it on GameStarted?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Serverside Random Number Generation

Postby JKillian » February 16th, 2013, 11:27 pm

Henrik wrote:Why not just put an instance on the Game and create it on GameStarted?


I guess you could, but when you have a few layers of classes it can be difficult for the deepest down stuff to get access to it. It's possible I guess to pass a Random instance in the constructor of everything, but it doesn't seem like that good of a design. Plus, I read Random isn't thread-safe and I think someone at Player.IO mentioned that there's a lot of threading going on, so that could potentially cause problems.
JKillian
 
Posts: 3
Joined: March 21st, 2012, 1:32 am

Re: Serverside Random Number Generation

Postby mstauber » February 19th, 2013, 3:06 pm

Creating a new Random object every time you need if, across several classes is very annoying. And since you can't use objects in a static-context when running on playerio's platform, you are kinda forced to... BUT, there is another way!

I jury rigged a C# language hack for you that should make that process a lot more wrist friendly. (note, don't advise actually using it as it's not really what some consider 'maintainable')

Code: Select all
public class rnd
{
    //override explicit casting int-to-rnd
    public static explicit operator rnd(int i)
    {
        rnd newRnd = new rnd();

        //generate new random number
        newRnd._randomNumber = (newRnd.m_multiplier * i + newRnd.m_increment) % newRnd.m_modulus;

        return newRnd;
    }
    //override implicit casting rnd-to-int
    public static implicit operator int(rnd r)
    {
        return r._randomNumber;
    }


    private int m_modulus = 2147483647;
    private int m_multiplier = 2147483629;
    private int m_increment = 2147483587;

    private int _randomNumber = 0;
}


//Example usage
static void Main(string[] args)
{
    int random = (rnd)6; //the number 6 is your seed :)

    while (true)
    {
        Console.WriteLine(random);
       

        //This first performs an explicit cast of int-to-rnd, which generates our 'rnd'
        //object with a random number who's seed was the int provided, then there is
        //an implicit cast from rnd-to-int, which returns the random number back to
        //the original int.
        random = (rnd)random;

        Console.ReadLine();
    }
}



I was able to run this on my development server, but didn't try it on the production... The development server does get quite moody when I try and use things like 'Reflection' or 'static-context', but it didn't seem to care about cast-overloading.

Anywho, enjoy :)
mstauber
 
Posts: 1
Joined: February 19th, 2013, 1:43 pm

Re: Serverside Random Number Generation

Postby robscherer123 » August 2nd, 2018, 9:28 pm

Bringing up an old topic here, but this has always been an issue for me. And designing my new game I'm trying to keep things as clean as possible, and the only solution I currently know of is having to pass around a reference to the main Random instance to every class that needs to have a random number generated (which is several dozen). I can't think of any alternative solutions either, as any solution I can think of would involve the use of static variables.

Here is my use case. I have plenty of things in my game such as enemies, blocks, items, etc that have unique names. So lets say "enemy423897". The server uses this name of the enemy to broadcast to clients so the client and server can be sure they are talking about the same enemy. Ideally, this would be how it's done:

Code: Select all
public class GameCode {
    for(int i = 0; i < 10; i++){
         var enemy:Enemy = new Enemy();
     }
}

public class Enemy {
     private string __name;

     public Enemy(){
          __name = PlayerIO.Random.Next(1, 999999);
     }
     
     public string name {
          get { return __name; }
     }
}

The above code would be the nice clean way of doing it if it were possible. However in my case, I'm forced to pass around references to the main random instance everywhere as so to avoid the same number being generated when generated in quick succession. Slowly but surely, I'm having to pass around references and it's making the code a bit uglier. That means if another class creates an enemy, I have to pass reference to that class, which then passes reference to the enemy class. And so on and so on, and it can feel a bit hack-ish.

Is there any solution that I'm not thinking of to implement? The nicest approach would be to have some type of Static method on the main GameCode class or PlayerIO class.
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm


Return to Multiplayer



cron