简体   繁体   English

javascript从多个范围获取随机数

[英]javascript getting random number from multiple ranges

I've seen different implementations for different languages, however I've yet to come across a js method. 我见过针对不同语言的不同实现,但是我还没有遇到js方法。

Essentially, I want to be able to retrieve a random value within a supplied array of ranges: var ranges = [{min:2,max:50},{min:500,max:600}, etc...]; 本质上,我希望能够在提供的范围数组内检索随机值: var ranges = [{min:2,max:50},{min:500,max:600}, etc...];

I have the basic min max function, just not sure how to efficiently do this: 我有基本的min max函数,只是不确定如何有效地做到这一点:

function getRandomNumber(min,max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

It's simple, you just need a for loop that iterates through all minimum and maximum values of an array and gets results from using the function you've provided: 很简单,您只需要一个for循环即可遍历数组的所有最小值和最大值,并通过使用提供的函数获取结果:

arrRange = [
    [15, 32],
    [9, 43],
    [8, 15]
]

function getRandomNumber(min,max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

for(i = 0; i < arrRange.length; i++) {
    min = arrRange[i][0];
    max = arrRange[i][1];
    randomNumber = getRandomNumber(min, max);
    console.log("A random number between " + min + " and " + max + " is " + randomNumber);
}

Results: 结果:

A random number between 15 and 32 is 28
A random number between 9 and 43 is 17
A random number between 8 and 15 is 15

You can use the forEach method for this. 您可以为此使用forEach方法。 The forEach() method calls a provided function once for each element in an array, in order. forEach()方法为数组中的每个元素依次调用提供的函数。 Read more about forEach at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach 在以下网址了解有关forEach的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

 var ranges = [{ min: 2, max: 50 }, { min: 500, max: 600 }]; ranges.forEach(function(e) { console.log(Math.floor((Math.random() * (e.max - e.min + 1)) + e.min)); }) 

The way this method works is that each range represents a fraction of the sum total of all possible values in all the ranges. 此方法的工作方式是,每个范围代表所有范围内所有可能值的总和的一小部分。 If n is the total number of all possible values, you can imagine the values in the ranges being distributed as follows: 如果n是所有可能值的总数,则可以想象分布范围内的值,如下所示:

1------10|11-----14|15---------------40|41-----49|....|(n-x)-----n
 range 1 | range 2 |       range 3     | range 4 |....|  range n

Where 1-10 corresponds to the values in range 1, and so on. 其中1-10对应于范围1中的值,依此类推。

From there on it is the simple matter of generating a random number from 1 to n , figuring out which range it belongs to and which value in that range it corresponds to. 从那里开始,很简单的事情是生成一个从1n的随机数,弄清楚它属于哪个范围,以及它对应于那个范围内的哪个值。

Let the ranges be represented as an array of tuples, as 让范围表示为元组数组,如下

ranges = [(2, 50), (500, 600), (630, 890)]

Assuming that the ranges are sorted and do not overlap each other, first we have to find the total number of integers spanning these ranges, ie the total number of possible values we can generate ( n ). 假设范围已排序且彼此不重叠,首先我们必须找到跨越这些范围的整数总数,即我们可以生成的可能值的总数( n )。 We store the lengths of these ranges in an array lengths . 我们将这些范围的长度存储在数组lengths Pseudocode: 伪代码:

lengths = ranges.map(range => range.max - range.min + 1)

Note that the map function specified above holds good only for inclusive ranges. 请注意,上面指定的map函数仅适用于包含范围。 Depending on the types of your ranges, you may have to change it. 根据范围的类型,您可能需要更改它。 Also note that n = sum(lengths) . 另请注意, n = sum(lengths)

Let x be a random number in the range 1 to n (inclusive). x1n (含)范围内的随机数。 Then the array index i of the range in which the x th integer is found is given as: 然后找到第x个整数的范围的数组索引i给出为:

i = 0
while (x > lengths[i]) {
    x -= lengths[i]
    i++
}

After this loop, x will contain the index of the random number in range i . 在此循环之后, x将包含范围i中的随机数的索引 That is, if x is 3, the random number is the third number in range i . 也就是说,如果x为3,则随机数是范围i的第三个数。

The required random number is then given by ranges[i].min + (x - 1) . 然后,所需的随机数由ranges[i].min + (x - 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM