Reputation:
Using Meteor, I need a method to generate the same random number on both the client and server, so I can get the benefits of the client method's latency compensation.
I noticed Meteor has a random package: http://docs.meteor.com/#random
I'm not familiar with random number generation or crytography and I don't really understand some of the Meteor documentation for the Random package, so I have some questions.
My problem definition in steps...
The alternative:
My app needs to repeatedly generate a lot of random numbers, so I'm trying to avoid all these round trips.
Upvotes: 2
Views: 2622
Reputation: 1612
What you're trying to do is insecure by nature. If you want security, call the server and wait for a response. The randomiser algorithm is available and sending the seed to the client or to server defeat this silly attempt of defence.
You should never trust data that comes from clients, always validate everything in a environment when you've complete control like your server.
Upvotes: 1
Reputation: 15711
It depends on what kind of thing you want to achieve, if we take the example of a slot machine in a MMO, what is done step by step is this:
This has one side effect: the result can be known by the user before he even starts spinning the wheel, but after he paid for it.
The problem is that if the user has different options to choose AFTER the random number is picked, he could choose the option with the lowest cost if he finds the number to be a bad one. To remedy to this, each random action needs to have their own number created.
You could see this schema detailed by a user who looked at sources of a game to find out he was "cheated" by the game: Zoot Loot is Not Random
In a flash game called clickerheroes, there are actions producing a random result. Since the game is saved once every couple minutes, and is not backed by a server, the random numbers are generated in advance. This prevents the trick to save, try your luck, load back, retry your luck as the same random number comes back...
Although this does not answer exactly to the OP, I hope it helps in the logic of providing trustworthy randoms to your users.
Upvotes: 1
Reputation: 8345
You can't seed (what you really want to do!) Math.random, and I don't think the Random package has support for it neither. You could use seedrandom.js, and seed it with the same on the server and the client.
Upvotes: 0