Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中,如何从多个数组中获得一个概率为%的随机对象?_Javascript - Fatal编程技术网

在Javascript中,如何从多个数组中获得一个概率为%的随机对象?

在Javascript中,如何从多个数组中获得一个概率为%的随机对象?,javascript,Javascript,假设我有3个对象数组: const fruits = [ {name: "Banana"}, {name: "Apple"}, {name: "Peach"} ] const car = [ {name: "Audi"}, {name: "Bentley"} ] const books = [ {name: "Alice in wonderland"}, {name

假设我有3个对象数组:

const fruits = [
{name: "Banana"}, 
{name: "Apple"}, 
{name: "Peach"}
]

const car = [
{name: "Audi"}, 
{name: "Bentley"}
]

const books = [
{name: "Alice in wonderland"}, 
{name: "Deep in the dark"}, 
{name: "Hunting Show"}
]
一个临时数组,我将在其中存储数组中的随机对象

const tempArray = []
我想从随机数组中得到随机对象

80%的几率从水果或汽车阵列中获得随机对象

我从books数组中获得随机对象的概率为20%

示例:Random chance为80%->数组中的Random对象被推送到tempArray并 tempArray应该有一个带有bane香蕉的对象

随机概率为20%->数组书中的随机对象被推送到tempArray,tempArray应该有名称搜索显示的对象


如何在javascript中实现这一点?

您需要获得两个随机数-第一个用来决定选择哪个组,第二个用来从中选择一个项目。为了给一组更高的被挑选的机会,第一个随机数需要在1-100范围内,以允许百分比,然后由该范围内的范围确定该组

例如,要使水果组有80%的机会被采摘,请执行以下操作:

函数getRandomNumbermin,max{ 返回Math.floorMath.random*max-min+1+min; } 常数=[ {名称:香蕉}, {名称:苹果}, {名称:Peach} ] 常数车=[ {姓名:奥迪}, {姓名:宾利} ] 常数书=[ {姓名:爱丽丝梦游仙境}, {name:deepinthedark}, {名称:狩猎表演} ] 常量tempArray=[]; 让groupChoice=GetRandomNumber1100; 如果groupChoice>20{ tempArray.pushfruits[getRandomNumber0,fruits.length-1].name; }否则{ tempArray.pushbooks[getRandomNumber0,books.length-1].name; }
console.logtempArray 你需要得到两个随机数——第一个决定挑选哪个组,第二个从中挑选一个项目。为了给一组更高的被挑选的机会,第一个随机数需要在1-100范围内,以允许百分比,然后由该范围内的范围确定该组

例如,要使水果组有80%的机会被采摘,请执行以下操作:

函数getRandomNumbermin,max{ 返回Math.floorMath.random*max-min+1+min; } 常数=[ {名称:香蕉}, {名称:苹果}, {名称:Peach} ] 常数车=[ {姓名:奥迪}, {姓名:宾利} ] 常数书=[ {姓名:爱丽丝梦游仙境}, {name:deepinthedark}, {名称:狩猎表演} ] 常量tempArray=[]; 让groupChoice=GetRandomNumber1100; 如果groupChoice>20{ tempArray.pushfruits[getRandomNumber0,fruits.length-1].name; }否则{ tempArray.pushbooks[getRandomNumber0,books.length-1].name; }
console.logtempArray 首先,我们将水果和汽车阵列结合起来。然后我们生成一个从1到100的随机数。从1到80的数字将从fruitsAndCar数组中选择一个随机元素,从81到100的数字将从books数组中选择一个随机元素

const fruits = [
    {name: "Banana"}, 
    {name: "Apple"}, 
    {name: "Peach"}
]

const car = [
    {name: "Audi"}, 
    {name: "Bentley"}
]

const books = [
    {name: "Alice in wonderland"}, 
    {name: "Deep in the dark"}, 
    {name: "Hunting Show"}
]

const tempArray = []
const fruitsAndCar = fruits.concat(car);

let randomNumber = Math.floor((Math.random() * 100) + 1); // 1 to 100

if (randomNumber <= 80) {
    let randomIndex = Math.floor(Math.random() * fruitsAndCar.length); // 0 to 4
    tempArray.push(fruitsAndCar[randomIndex]);
} else {
    let randomIndex = Math.floor(Math.random() * books.length); // 0 to 2
    tempArray.push(books[randomIndex]);
}

console.log('tempArray: ' + JSON.stringify(tempArray));

首先,我们将水果和汽车阵列结合起来。然后我们生成一个从1到100的随机数。从1到80的数字将从fruitsAndCar数组中选择一个随机元素,从81到100的数字将从books数组中选择一个随机元素

const fruits = [
    {name: "Banana"}, 
    {name: "Apple"}, 
    {name: "Peach"}
]

const car = [
    {name: "Audi"}, 
    {name: "Bentley"}
]

const books = [
    {name: "Alice in wonderland"}, 
    {name: "Deep in the dark"}, 
    {name: "Hunting Show"}
]

const tempArray = []
const fruitsAndCar = fruits.concat(car);

let randomNumber = Math.floor((Math.random() * 100) + 1); // 1 to 100

if (randomNumber <= 80) {
    let randomIndex = Math.floor(Math.random() * fruitsAndCar.length); // 0 to 4
    tempArray.push(fruitsAndCar[randomIndex]);
} else {
    let randomIndex = Math.floor(Math.random() * books.length); // 0 to 2
    tempArray.push(books[randomIndex]);
}

console.log('tempArray: ' + JSON.stringify(tempArray));

很可能const chance=Math.random会给你一个从0到1的数字,从中你可以检查chance>0.2,然后将car或fruit推到临时数组,否则将books推到临时数组很可能const chance=Math.random会给你一个从0到1的数字,因此,您可以检查chance>0.2,然后将car或fruit推到临时数组,否则将books推到临时数组中,但请记住,这仅适用于两组-fruits和books。如果您还想包括car组,则需要更新If测试以允许其他范围snp-但请记住,这仅适用于两个组-水果和书籍。如果您还想包括汽车组,则需要更新If测试以允许其他范围