Home Ask Login Register

Developers Planet

Your answer is one click away!

Aran Bins February 2016

My Random Number Generator is producing small numbers

I'm trying to create a "random" number generator for my online game.

I want to create the number purely based on positions and stats of players, and also based on a small counter that increments by 1 each step in my controller.

Here's my code to create a "seed" and another function to use the seed to generate a random number from 0 -> max non-inclusive:

function generate_seed(){
    var num = 1;
    for(var i = 0; i < number_of_players; i++){
        var prevNum = num;

    num+=obj_controller.randStep; //randStep is a variable that gets incremented by 1 in my controller object each step

    //Loops through all players

        if(x % 2){num+=x;}
        else {num-=x;}

        if(y % 2){num+=y;}
        else {num-=y;}

        if(hp % 2){num+=hp;}
        else {num-=hp;}

        if(randStep % 2){num+=randStep;}
        else {num-=randStep;}


    return abs(num);

function random(var max){
    //Generate synced random number from 0 -> max, does not include max
    var seeder = synced_random_generate();

    seeder+= max * 5; //Make sure the seed is greater than max

    return seeder mod max;

The thing is, this sort of works, but if I do: random(20000);

It will usually return a fairly small number relative to the max (20,000).

I've never seen it return a number greater than 3000. Which is very strange.

Does anyone know what's wrong or a possible simpler way to generate a random number based on player positions/stats and an incrementing counter in my controller?


Aran Bins February 2016

Found a really great solution online:

int rand1(int lim)
        static long a = 100001;
        a = (a * 125) % 2796203;
        return ((a % lim) + 1);

Post Status

Asked in February 2016
Viewed 3,551 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life