php随机选择,php – 从4个表中随机选择

我认为这不仅仅是一个逻辑问题,而是一个关于处理表格的技术问题.如果您先处理逻辑,那么您可以轻松地在桌面上执行所需的操作.我们只需要知道随机选择将使用(销毁)每个项目的数量.这是一个随机选择方法部分sudo-code / part php:

1. Query database for available items and their relative values.

2. Store information as a multi-dimensional Array

3. Shuffle the Array

//in php

bool shuffle ( array $itemsArray() )

4. Iterate through each item in the array and add 1

to a variable for that item if, we have not reached our

limiting factors (amount available and cost vs remaining points).

Do this until all available points are allotted to a variable.

//in php

$i = 0;

do {

if ($itemsArray[$i][numAvail] > 0 &&

($availiblePoints - $itemsArray[$i][cost] >= $itemsArray[$i][cost]){

$$itemsArray[$i]++

//use of '$$' for variable variable

$availiblePoints-=$itemsArray[$i][cost];

}

else {

countSkips++

//need to keep track of how many items we skip

//if $availiblePoints is not zero yet but skips is size of array then

//we are done and have leftover points that cant be used.

}

$i++;

if ($i > count($itemsArray)) { $i=0; };

//start over if we have gone past the end of our Array

} while ($availiblePoints > 0 && $countSkips < count($itemsArray) );

5. Logic Done. Now use the new variables to perform action on tables

因为数组是随机洗牌的,所以无论我们有多少分数,我们的结果都是随机的.如果我们有100分,我们的随机数组中的第一项成本为100分,或前4分为25分;随机性将以任何方式完成其工作.

这只是概念.代码可以改进一堆,例如我们保存的变量实际上应该在一个数组中,这样我们就可以在它对表执行操作时循环遍历它们.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值