2

这就是我想要做的:从 20-30 个用户 ID 的列表中创建一个包含 100 个随机值的数组。我需要使每个用户的数量尽可能相等。(如果有 25 个用户,每个用户将在数组中有 4 个点,(100 / 25 = 4)并且顺序需要是随机的。

以包含 10 个和 3 个用户 ID 的数组为例:该数组可以读取 (3,2,3,1,2,1,2,3,1,2,1)。这里有三个 3、三个 2 和四个 1。在填充十个数组的同时尽可能相等。

请帮帮我...

这是一个如何部署它的模型 http://fitzpicks.com/squarebet.php

在模型中,我使用电子表格创建的字符串来填充单元格。

ps 我刚开始使用 html、css 和 php,所以请不要用你的超级黑客技能取笑或破坏我的网站!

干杯格雷格

4

3 回答 3

1

我认为您的问题可以通过这种简单的方式解决:您需要将您的 IDs 数组重复到所需的数量,然后对其进行随机播放。可能有一个“尾巴”(如果total count/count of IDs不是整数) - 为了做出更好的随机性,我建议从原始数组中检索随机 ID。这是一个示例:

$rgIds  = [5, 72, 10, 93];
$iCount = 30;

$iLoop    = (int)($iCount/count($rgIds)); //count of repeats
$rgResult = array_intersect_key( //'tail' from random Ids
               $rgIds, 
               array_flip(array_rand($rgIds, $iCount%count($rgIds))));
for($i=0; $i<$iLoop; $i++)
{
   $rgResult=array_merge($rgResult, $rgIds);
}
shuffle($rgResult);

此示例将通过此测试产生:

var_dump($rgResult, array_count_values($rgResult));

在以下输出中:

数组(30){
  [0]=>
  整数(93)
  [1]=>
  整数(93)
  [2]=>
  整数(5)
  [3]=>
  整数(93)
  [4]=>
  整数(10)
  [5]=>
  整数(72)
  [6]=>
  整数(10)
  [7]=>
  整数(5)
  [8]=>
  整数(72)
  [9]=>
  整数(10)
  [10]=>
  整数(5)
  [11]=>
  整数(93)
  [12]=>
  整数(72)
  [13]=>
  整数(5)
  [14]=>
  整数(72)
  [15]=>
  整数(10)
  [16]=>
  整数(5)
  [17]=>
  整数(10)
  [18]=>
  整数(93)
  [19]=>
  整数(93)
  [20]=>
  整数(93)
  [21]=>
  整数(72)
  [22]=>
  整数(5)
  [23]=>
  整数(93)
  [24]=>
  整数(72)
  [25]=>
  整数(72)
  [26]=>
  整数(10)
  [27]=>
  整数(5)
  [28]=>
  整数(10)
  [29]=>
  整数(72)
}
数组(4){
  [93]=>
  整数(8)
  [5]=>
  整数(7)
  [10]=>
  整数(7)
  [72]=>
  整数(8)
}
于 2013-08-23T08:18:39.600 回答
1

这是shuffle 的 PHP 手册条目中的一个示例:

<?php
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
    echo "$number ";
}
?>

在您的情况下,如果您想使用初始数组中的值四次,您可以执行以下操作:

$ids = array(101, 102, 109, 110);
$random = array_merge($ids, $ids, $ids, $ids);
shuffle($random);
foreach ($random as $id) {
  echo "$id ";
}

我认为您可以只使用$random数组中的前 100 个项目。

将来,您可以查看PHP 手册中的所有数组函数以获取合适的函数。

于 2013-08-23T08:19:32.653 回答
0

我认为这是你需要的:

$ids = array(1,2,3,4);
$count = 10;

// get number of full sets
$full_sets_number = floor($count/count($ids));

// number of ids in last not full set
$rest_ids_number = $count % count($ids);

// get random ids that will occur more frequently
$rest_ids = $ids;
shuffle($rest_ids);
$rest_ids = array_slice($rest_ids, 0, $rest_ids_number);

// fill result array with ids
$result_array = array();
for($i = 0; $i < $full_sets_number; $i++)
{
    $result_array = array_merge($result_array, $ids);
}
$result_array = array_merge($result_array, $rest_ids);

// shuffle it
shuffle($result_array);

var_dump($result_array);

上面代码的输出是这样的:

array(10) { [0]=> int(4) [1]=> int(2) [2]=> int(1) [3]=> int(1) [4]=> int(3) [5]=> int(1) [6]=> int(4) [7]=> int(4) [8]=> int(2) [9]=> int(3) }
于 2013-08-23T08:41:55.783 回答