Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.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_Php_Arrays - Fatal编程技术网

Javascript 基于机会期权的随机值生成

Javascript 基于机会期权的随机值生成,javascript,php,arrays,Javascript,Php,Arrays,我想要一种从数组中选取随机元素的方法,其中选取元素的概率表示为每个元素的百分比 数组可以是JSON格式,也可以是PHP数组,不过代码必须用PHP编写。以下是JSON中的一个示例: { "extreme": { "name": "item 1", "chance": 1.0 }, "rare": { "name": "item 2", "chance": 9.0 }, "ord

我想要一种从数组中选取随机元素的方法,其中选取元素的概率表示为每个元素的百分比

数组可以是JSON格式,也可以是PHP数组,不过代码必须用PHP编写。以下是JSON中的一个示例:

{
    "extreme":  {
        "name":   "item 1",
        "chance": 1.0
    },
    "rare":     {
        "name":   "item 2",
        "chance": 9.0
    },
    "ordinary": {
        "name":   "item 3",
        "chance": 90.0
    }
}
对于上述示例,以下是正确的:

  • 项目1
    extreme
    )应从100次中选择1次
  • 项目2
    稀有
    )应从100次中选择9次
  • 第3项
    普通
    )应从100次中选择90次
简单地说:用于从数组或JSON字符串中随机选取项目的代码,并设置每个项目的百分比概率。

将更快。

但我会留下我的记录:

<?php

$json_string = '
{ "extreme":{
    "name":"item 1",
    "chance":1.0
  },
  "rare":{
    "name":"item 2",
    "chance":9.0
  },
  "ordinary":{
    "name":"item 3",
    "chance":90.0
  }
}';

$data = json_decode($json_string, true);

$arr = array();

// Cycle through "extreme", "rare" and "ordinary"
foreach($data as $item){
    for($i=0; $i<$item['chance']; $i++){
        // Add the item's name to the array, [chance] times
        array_push($arr, $item['name']);
    }
}

shuffle($arr); // shuffle the array

$chosen_item = $arr[array_rand($arr)]; // Result

echo $chosen_item;

?>
会更快。

但我会留下我的记录:

<?php

$json_string = '
{ "extreme":{
    "name":"item 1",
    "chance":1.0
  },
  "rare":{
    "name":"item 2",
    "chance":9.0
  },
  "ordinary":{
    "name":"item 3",
    "chance":90.0
  }
}';

$data = json_decode($json_string, true);

$arr = array();

// Cycle through "extreme", "rare" and "ordinary"
foreach($data as $item){
    for($i=0; $i<$item['chance']; $i++){
        // Add the item's name to the array, [chance] times
        array_push($arr, $item['name']);
    }
}

shuffle($arr); // shuffle the array

$chosen_item = $arr[array_rand($arr)]; // Result

echo $chosen_item;

?>

另一种方法是:

$options = [
    "extreme"  => [
        "name"   => "item 1",
        "chance" => 1.0,
    ],
    "rare"     => [
        "name"   => "item 2",
        "chance" => 9.0,
    ],
    "ordinary" => [
        "name"   => "item 3",
        "chance" => 90.0,
    ]
];

$rand = rand(0, 99);
$max = 0;

foreach ($options as $option) {
    $max += $option['chance'];
    if ($rand < $max) {
        break;
    }
}
echo $option['name'], PHP_EOL;
$options=[
“极端”=>[
“名称”=>“第1项”,
“机会”=>1.0,
],
“稀有”=>[
“名称”=>“第2项”,
“机会”=>9.0,
],
“普通”=>[
“名称”=>“第3项”,
“机会”=>90.0,
]
];
$rand=rand(0,99);
$max=0;
foreach($options作为$option){
$max+=$option['chance'];
如果($rand<$max){
打破
}
}
echo$option['name'],PHP_EOL;

另一种方法:

$options = [
    "extreme"  => [
        "name"   => "item 1",
        "chance" => 1.0,
    ],
    "rare"     => [
        "name"   => "item 2",
        "chance" => 9.0,
    ],
    "ordinary" => [
        "name"   => "item 3",
        "chance" => 90.0,
    ]
];

$rand = rand(0, 99);
$max = 0;

foreach ($options as $option) {
    $max += $option['chance'];
    if ($rand < $max) {
        break;
    }
}
echo $option['name'], PHP_EOL;
$options=[
“极端”=>[
“名称”=>“第1项”,
“机会”=>1.0,
],
“稀有”=>[
“名称”=>“第2项”,
“机会”=>9.0,
],
“普通”=>[
“名称”=>“第3项”,
“机会”=>90.0,
]
];
$rand=rand(0,99);
$max=0;
foreach($options作为$option){
$max+=$option['chance'];
如果($rand<$max){
打破
}
}
echo$option['name'],PHP_EOL;

您的问题不是很清楚。你举了一个变量的例子,但我们并不知道它们是什么,以及它们是如何确定的。请更详细地解释你的问题(你想做什么),并举例说明结果。If最好是提供所需的伪代码。@blex简单地说就是从数组或JSON文件中随机选取一个项目,并设置每个项目的概率百分比。好的,您能提供一个JSON示例,这样我们就可以看到这些概率是如何声明的吗?
{“extreme”:{“name”:“item 1”,“chance”:1.0}你的问题不是很清楚。你举了一个变量的例子,但我们并不知道它们是什么,以及它们是如何确定的。请更详细地解释你的问题(你想做什么),并举例说明结果。If最好是提供所需的伪代码。@blex简单地说就是从数组或JSON文件中随机选取一个项目,并设置每个项目的概率百分比。好的,您能提供一个JSON示例,这样我们就可以看到这些概率是如何声明的吗?
{“extreme”:{“name”:“item 1”,“chance”:1.0},“稀有”:{“名称”:“项目2”,“机会”:9.0},“普通”:{“名称”:“项目3”,“机会”:90.0}
我也想过这样做:)+1我也想过这样做:)+1