Search Unity

Weighted Random Number from a Range.

Discussion in 'Scripting' started by Riderfan, Jul 29, 2019.

  1. Riderfan

    Riderfan

    Joined:
    Jan 10, 2013
    Posts:
    514
    Hello.

    I'm trying to wrap my head around obtaining a random number from a range (enumeration) that is weighted to one end of the range or the other.

    - My character has a rank of 1 to 100.
    - My character has a collection of attributes with a range of 0 to 6 (an enum of Extremely Low to Extremely High).

    I'm trying to create a method that will take three parameters, the min and max of the range, and the weight that is the player's rank. The closer the player is to being ranked 100, the closer the random number from the range will be Extremely Low, and vice versa. The closer to being ranked #1, the closer the random number from the range will be Extremely High.

    I've looked at a couple of Unity assets on the store but I'm not finding one that seems to do what I'm after. If anyone has any suggestions, or knows how to generate a method that does this, I'd appreciate the help.

    thanks
     
  2. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    I ve done a quick google search and https://forum.unity.com/threads/random-numbers-with-a-weighted-chance.442190/ came up. Basically Kiwasi is telling him to use an animation curve. This does sound like something useful. You set up the curve in a way where the rank of the player is the top of the graph and flatten in out at the top to give it more chance to get that value (y-value of the graph). Than you generate a random value in the range of your ranks + the total amount of occurances you have added. So certain values can be found more than others.

    Alternatively you can make a dictionary<int, int> and store your values along with their chance to appear. Than make a list with all the numbers, adding all the dictionary entries the amount of times you want them to occur and get a random value from that list (maybe scramble them, but shouldnt be needed).Get that value and thats more or less a weighted random as I would do it. Might get a bit more complicated with it being dynamic about the rank of the player but the idea should work.

    You could also just write an algorithm to increase the chances when you get closer to the player rank. But for the math on that one I cant rly help. Though it shouldnt be that complicated.
     
    theforgot3n1 likes this.
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Use exponential functions, and numbers from 0 to 1 - for example raising the value to its square or by four. If it’s close to one, it’ll remain there, the further it gets away from one towards zero, the quicker the Fall-off. If you have the inclination, and high-school Level math knowledge, you can curtail the function to exactly hit the spots you want.
     
  4. KaiMiraGames

    KaiMiraGames

    Joined:
    Jan 2, 2020
    Posts:
    17
    eisenpony likes this.