Generate number on server and send all clients


Hello. I am developing a multiplayer game. I have a problem. I want the server to create a random number from 1 to 2. And send this number to all customers. But if clients play once, the server must regenerate the number
I wrote such a function. She chooses a number from 1 to 2. And then always returns this number. If you restart the server, it will return a different number.
project -

var map_positions = [
{ map: 1},
{ map: 2},
var vibrana_map = { map: 0};
function locateFood(data) {
var roomId = data;
var tmpName = room + roomId;
if ( == 0) {
var map_num = Math.floor(Math.random() * 2);
vibrana_map = map_positions[map_num];“foodPosition”, vibrana_map);
else {“foodPosition”, vibrana_map);


Hi again @goboju,
For generating number between 1, 2 you can use this function from phaser’s source code

var FloatBetween = function (min, max)
    return Math.random() * (max - min) + min;

But as far as i can see you dont need to generate a number for picking an element from Array, instead you can use this:

var GetRandom = function (array, startIndex, length)
    if (startIndex === undefined) { startIndex = 0; }
    if (length === undefined) { length = array.length; }

    var randomIndex = startIndex + Math.floor(Math.random() * length);

    return (array[randomIndex] === undefined) ? null : array[randomIndex];

This one is also from phaser, Phaser.Utils.Array.GetRandom you can check other’s from link.

If function locateFoot working everytime a user does its move, then it should work, can you again share the github link so that i can check there too :slight_smile:


Thanks for the answer. Can you put this into a project? I need every time when entering the game the number drops from 1 to 2. Now, if you do not restart the server, the same number will fall out