Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Randomly Distribute Numbers

I am creating procedural generation for my game. At which I have randomly generated rooms that have an enemy "budget" based on the amount of ground tiles. I want the enemies to spawn in several groups per room. A spawner script will do this but I want it to have a randomly distributed budget from the room it is in. I want it to be also even-ish in distribution. This has stopped me for a long time, any help?

Thanks in advance.

Best Answers

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    edited July 2020 Accepted Answer

    Do something like

    Int Total = 100;
    Int difference  = (totalBudget/totalRooms) + Random.Range(0,10);
    //in your example it whould be 20 to 30
    Int a=0,b=0,c=0,d=0,e=0;
    while(a+b+c+d+e >= total){
        a= Random.Range(0,difference);
        b= Random.Range(0,difference);
        c= Random.Range(0,difference);
        d= Random.Range(0,difference);
        e= Random.Range(0,difference);
    }
    


  • Accepted Answer

    Wait, that won't work. It will always give the same numbers.

    Instead we can do this:

    while(a+b+c+d+e != total){
    a = total / 5 + Random.Range(-5, 5);
    b = total / 5 + Random.Range(-5, 5);
    c = total / 5 + Random.Range(-5, 5);
    d = total / 5 + Random.Range(-5, 5);
    e = total / 5 + Random.Range(-5, 5);
    }
    


    That will do it.

Answers

  • Try

    Budget = Random.Range(min,max);

  • Answer above will do the thing. If you want even-ish - just run in cycles until it will generate even number.


    int randomNumber;

    do{

    randomNumber = Random.Range(min, max);

    }while(randomNumber%2 != 0);


    It will run until it will generate even number. But you need to be sure min-max range contains at least one even number.


    If range in not very big you can collect all even numbers in array and than randomly select one from it.

  • I meant that each group will have a close "budget" to another one like if there were 5 groups something like this will happen:

    Total room budget is 100. The groups will get:

    17, 24, 23, 17, 19


    I wanted something like that.

  • Thanks mate!

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    edited July 2020

    It didn't worked because there was a typo

    I said totalRooms/totalbudget

    But i was gonna say

    TotalBudget/totalRooms


    So try it again with this

Sign In or Register to comment.