简体   繁体   English

创建一个返回随机整数但具有指定分布/“权重”的Javascript函数

[英]Creating a Javascript function that returns random integers but with a specified distribution/“weight”

I have an array of values: 我有一个值数组:

var my_arr = [/*all kinds of stuff*/]

I have a function that generates a random number, which I use as the index of an element in my_arr ... 我有一个生成随机数的函数,用作my_arr元素的my_arr ...

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

...so I could do stuff like this: ...所以我可以做这样的事情:

my_arr[RandomFromRange(0,my_arr.length)];

What I want to do is to designate certain elements within my_arr as having "priority", so that RandomFromRange returns 5, say, 25% of the time, returns 4, 14% of the time, and returns any other number... 我想做的是将my_arr某些元素指定为具有“优先级”,以便RandomFromRange返回5(例如25%的时间),返回RandomFromRange %的时间并返回任何其他数字...

(100 - 25 - 14)/(my_arr.length - 2)

...% of the time. ...% 的时间。

As I was doing my research, I came across several posts that describe similar problems , but their answers are not in Javascript, and I, alas, don't have enough math to understand their general principles. 在进行研究时,我遇到了描述 类似问题的几篇文章 ,但是他们的答案都没有用Javascript编写,可惜我没有足够的数学知识来理解它们的一般原理。 Any advice would be appreciated. 任何意见,将不胜感激。

This may not be as exact as what you are looking for, but this certainly works. 这可能与您要查找的内容不完全一样,但是确实可以。 Basically this code returns a random number specified from the min and max like yours, but only after addressing the priority numbers based on the chance given. 基本上,此代码返回一个从最小值和最大值中指定的随机数,就像您的代码一样,但是仅在根据给定的机会处理了优先级数字之后才返回。

First we gotta prioritize your priority numbers in the code. 首先,我们要优先处理代码中的优先级数字。 If there is no hit on your priority numbers, that is when we proceed to the normal RNG. 如果您的优先级号码没有任何问题,那就是我们继续进行常规的RNG。

 //priority = list of numbers as priority, //chance = the percentage //min and max are your parameters var randomFromRange = function (min,max,priority,chance) { var val = null; //initialize value to return for(var i = 0; i < priority.length; i++){ //loop through priority numbers var roll = Math.floor(Math.random()*100); //roll the dice (outputs 0-100) if(chance > roll){ ///check if the chance is greater than the roll output, if true, there's a hit. Less chance value means less likely that the chance value is greater than the roll output val = priority[i]; //make the current number in the priority loop the value to return; break; //if there's a hit, stop the loop. } else{ continue; //else, keep looping through the priority list } } //if there is no hit to any priority numbers, return a number from the min and max range if(val == null){ val = Math.floor(Math.random()*(max-min+1)+min); } //return the value and do whatever you want with it return val; }; document.getElementsByTagName('body')[0].onclick = function (){ console.log(randomFromRange(0,10,[20,30],50)); } 
 <!DOCTYPE html> <html> <body style='height: 1000px; width: 100%;'></body> <script></script> </html> 

This code applies a single chance on all array of priority numbers. 此代码对优先级的所有数组应用一次机会。 If you want individual chances for each number in the priority list, we gotta modify the structure and change parameters to a single array of objects that holds something like 如果您希望优先级列表中的每个数字都有机会,我们必须修改结构并将参数更改为包含以下内容的单个对象数组

var priorityList = [{num: 4, chance: 25},
                    {num: 5, chance: 12}]

etc 等等

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

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