Get a random number focused on center

后端 未结 20 2987
离开以前
离开以前 2020-12-12 09:28

Is it possible to get a random number between 1-100 and keep the results mainly within the 40-60 range? I mean, it will go out of that range rarely, but I want it to be main

相关标签:
20条回答
  • 2020-12-12 09:30

    The simplest way would be to generate two random numbers from 0-50 and add them together.

    This gives a distribution biased towards 50, in the same way rolling two dice biases towards 7.

    In fact, by using a larger number of "dice" (as @Falco suggests), you can make a closer approximation to a bell-curve:

    function weightedRandom(max, numDice) {
        let num = 0;
        for (let i = 0; i < numDice; i++) {
            num += Math.random() * (max/numDice);
        }    
        return num;
    }
    

    Weighted random numbers

    JSFiddle: http://jsfiddle.net/797qhcza/1/

    0 讨论(0)
  • 2020-12-12 09:32

    I'd recommend using the beta distribution to generate a number between 0-1, then scale it up. It's quite flexible and can create many different shapes of distributions.

    Here's a quick and dirty sampler:

    rbeta = function(alpha, beta) {
     var a = 0   
     for(var i = 0; i < alpha; i++)   
        a -= Math.log(Math.random())
    
     var b = 0   
     for(var i = 0; i < beta; i++)   
        b -= Math.log(Math.random())
    
      return Math.ceil(100 * a / (a+b))
    }
    
    0 讨论(0)
  • 2020-12-12 09:35

    It looks stupid but you can use rand twice:

    var choice = Math.random() * 3;
    var result;
    
    if (choice < 2){
        result = Math.random() * 20 + 40; //you have 2/3 chance to go there
    }
    else {
        result = Math.random() * 100 + 1;
    }
    
    0 讨论(0)
  • 2020-12-12 09:35

    Sure it is possible. Make a random 1-100. If the number is <30 then generate number in range 1-100 if not generate in range 40-60.

    0 讨论(0)
  • 2020-12-12 09:36

    You have some good answers here that give specific solutions; let me describe for you the general solution. The problem is:

    • I have a source of more-or-less uniformly distributed random numbers between 0 and 1.
    • I wish to produce a sequence of random numbers that follow a different distribution.

    The general solution to this problem is to work out the quantile function of your desired distribution, and then apply the quantile function to the output of your uniform source.

    The quantile function is the inverse of the integral of your desired distribution function. The distribution function is the function where the area under a portion of the curve is equal to the probability that the randomly-chosen item will be in that portion.

    I give an example of how to do so here:

    http://ericlippert.com/2012/02/21/generating-random-non-uniform-data/

    The code in there is in C#, but the principles apply to any language; it should be straightforward to adapt the solution to JavaScript.

    0 讨论(0)
  • 2020-12-12 09:37

    What about using something like this:

    var loops = 10;
    var tries = 10;
    var div = $("#results").html(random());
    function random() {
        var values = "";
        for(var i=0; i < loops; i++) {
            var numTries = tries;
            do {
                var num = Math.floor((Math.random() * 100) + 1);
                numTries--;
            }
            while((num < 40 || num >60) && numTries > 1)
            values += num + "<br/>";
        }
        return values;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="results"></div>

    The way I've coded it allows you to set a couple of variables:
    loops = number of results
    tries = number of times the function will try to get a number between 40-60 before it stops running through the while loop

    Added bonus: It uses do while!!! Awesomeness at its best

    0 讨论(0)
提交回复
热议问题