Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Weighted Random Numbers - Generate Randon Numbers with Weighted Chances

Discussion in 'Assets and Asset Store' started by LukasKallenbach, Aug 4, 2015.

  1. LukasKallenbach

    LukasKallenbach

    Joined:
    Oct 10, 2012
    Posts:
    12
    Hi,

    I've created this random number generator that lets you adjust the statistical distribution of the numbers in generates using an AnimationCurve.

    It comes in handy when working with procedurally generated content - for randomizing e.g. the size of in-game objects, distances between obstacles, maybe even damage or the amount of gold coins you get when you open that treasure chest?

    The idea is that the AnimationCurve then becomes a game design tool that lets you adjust the probabilities without having to code.

    Webplayer Demo


    Asset Store link:
    https://www.assetstore.unity3d.com/en/#!/content/38858

    Screenshot of how it looks like in the Unity Editor:




    At this point, floats and integers can be randomized. Feedback, constructive criticism and ideas what else this could be used for are appreciated!
     
  2. rayfigs

    rayfigs

    Joined:
    Feb 8, 2009
    Posts:
    41
    HI , I was looking at your assets and wanted to know if this and your object randomizer essentially be interchangeable, they seem similar.
     
  3. LukasKallenbach

    LukasKallenbach

    Joined:
    Oct 10, 2012
    Posts:
    12
    Hi,
    while the packages are both for randomizing things, they are not interchangeable - Weighted Random Numbers is specifically for randomizing numbers, it returns random float and int values according to a statistical distribution.

    The Object Randomizer (link) is not made for randomizing numbers, but Unity objects. So if you'd like to e.g. randomize the enemies that spawn in your game, with some types of enemies being more common that others, or the texture something has, the Object Randomizer let's you create a list in the inspector where you can drag those enemies (Prefabs or GameObjects) or Textures and adjust their probabilities.
    [Technically, it can be used to randomize anything that inherits from UnityEngine.Object.]
     
  4. rayfigs

    rayfigs

    Joined:
    Feb 8, 2009
    Posts:
    41
    So with Weighted Random Numbers, I'd be able to call the weighted random number generator by one of my scripts and it will return a value within my specified range and with the specified weights? I am looking for a solution to help me randomize the generation of object but already have the logic which uses the current rand function, I just need to be able to choose probabilities of each type, so I take it either of the two would work for me. With both assets, would I be able to set up the lists via script or do I need to set everything up in the inspector?

    Are the docs available to view?

    Thanks,
    Reinaldo
     
  5. LukasKallenbach

    LukasKallenbach

    Joined:
    Oct 10, 2012
    Posts:
    12
    I've made the current instruction files available online:
    Weighted Random Numbers | Object Randomizer

    Yes, Weighted Random Numbers let's you specify range and weights of the numbers it returns when called. This is done by editing a statistical distribution curve using Unity's AnimationCurve editor, here a screenshot how it looks like:

    Example Curve Cropped.png
    The range of the curve determines the range of possible numbers, so in that screenshot, it would be from 0 - 100 and numbers have a higher weighting the closer they are to 50.

    If you have a list of objects you would like to return with weighted chances, the Object Randomizer might be a better fit for that purpose, as it lets you create a list of objects/probabilities like this:

    bar example small.png

    However, if you already have your own logic for that and only need a random number generator with weighted probabilities, the Weighted Random Number Package could probably also make it work.

    Both assets are created so that the probabilites are edited in the inspector. At least for the Object Randomizer I could add an API to change probabilities by script in the future, if there is a demand for that.
     
  6. rayfigs

    rayfigs

    Joined:
    Feb 8, 2009
    Posts:
    41
    I tried giving the weighted Random numbers a go. Would you see any options open to manipulating the keys on the animation curve via script? I have different objects which are unlocked per each level. I would like to have the possibilities of the spawned object to be 100% of the newly unlocked object, then modify the probabilities again based on my logic.

    It seems by your response, that the object randomizer could expose an API to do this, if the demand is there. Is this something that would take long to implement?

    Thanks,
    Reinaldo