<?php

/* *
* 通过一个标识,从一个数组中随机获取固定数据
* $arr 数组
* $num 获取的数量
* $time 随机固定标识值,一般用固定时间或者某个固定整型
* */
function getRandFixedData($arr, $num, $time = 0)
{
if(empty($arr)){
return $arr;
}
  $time = abs($time);   //获取绝对值,防止出现负数
//数组的个数
$listcount = count($arr);
/*if($listcount < $num){
//数组总数不能少于需要获取的数量
return [];
}*/
$num = $listcount < $num ? $listcount:$num;
//分成$num 个数组每一个数组是多少个元素
$parem = floor($listcount / $num);
//分成$num 个数组还余多少个元素
$paremm = $listcount % $num;
$start = 0;
$new_array = [];
for ($i = 0; $i < $num; $i++) {
$end = $i < $paremm ? $parem + 1 : $parem;
$new_array[$i] = array_slice($arr, $start, $end);
$start = $start + $end;
}
if($time%2 == 1){
//固定几率逆向排序
$new_array = array_reverse($new_array);
}
$result = [];
for ($j = 0; $j < $num; $j++) {
$increase_num = $time+$j;
$index = $increase_num % count($new_array[$j]);
if($increase_num%3 == 1){
//固定几率逆向排序
$new_array[$j] = array_reverse($new_array[$j]);
}
$result[] = $new_array[$j][$index];
} return $result;
} //使用方法
$time = isset($_GET['time'])?$_GET['time']:123456789;
$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
$test1 = getRandFixedData($arr,5,$time);
echo '<pre>';
print_r($test1); $data = [1,2];
foreach ($data as $k=>$v){
$test2 = getRandFixedData($arr,5,$time+$k);
echo '<pre>';
print_r($test2);
} die;

最新文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (9) -----第二章 实体数据建模基础之继承关系映射TPH
  2. Android 抽屉效果的导航菜单实现
  3. Android LogCat使用详解
  4. 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 事件 ID: 7000
  5. python之列表、字典的使用
  6. 查GDI对象泄露的利器:GDIView
  7. Make Things Move -- Javascript html5版(三)三角函数形式的动画
  8. TortoiseGit - pull request
  9. Rxjava observeOn()和subscribeOn()初探
  10. 最优化算法:BFGS算法全称和L-BFGS算法全称
  11. Debian安装fail2ban来防止扫描
  12. CSS3使用transition属性实现过渡效果
  13. extjs ajax 同步 及 confirm 确认提示框问题
  14. [转载]Javascript .then()这个方法是什么意思?
  15. es6generator
  16. P4609 [FJOI2016]建筑师
  17. 【Linux】 Ncures库的介绍与安装
  18. 使用kubectl创建部署
  19. textmate常用快捷键备忘
  20. 在recycler中写的布局不起作用

热门文章

  1. linux查找并替换命令
  2. POI导出Excel发现不可读取的内容
  3. geotrellis使用(四十一)流水线技术
  4. 腾讯云云机安装dockers
  5. 【C++/函数】实验2
  6. 唉,可爱的小朋友---(DFS)
  7. java-03-动手动脑
  8. springboot+mybatis+druid数据库连接池
  9. eclipse与hadoop-eclipse-plugin之间的版本对应关系
  10. qt 操作注册表,设置ie代理