我现在有这个:
$from = $_POST['from'];
$q = mysql_query("SELECT * FROM puzzles LIMIT $from,1");
while(($row=mysql_fetch_assoc($q))!=false){
$result = array('description' => $row['description'], 'id' => $row['id']);
echo json_encode($result);
问题是,我得到的绿色比蓝色更多,例如:var generateIcons = function(playtime, players) {
var count = playtime / 2;
icons = [];
for (var i = 1; i <= count; i++) {
if (i % 8 === 0 && players === 8) {
icons.push("cyan");
}
else if (i % 7 === 0 && players >= 7) {
icons.push("violet");
}
else if (i % 6 === 0 && players >= 6) {
icons.push("orange");
}
else if (i % 5 === 0 && players >= 5) {
icons.push("black");
}
else if (i % 4 === 0 && players >= 4) {
icons.push("gold");
}
else if (i % 3 === 0 && players >= 3) {
icons.push("red");
}
else if (i % 2 === 0 && players >= 2) {
icons.push("blue");
}
else {
icons.push("green");
}
}
};
如何获得〜等量的绿色,蓝色,红色,金色?
答案 0 :(得分:0)
所以,如果我理解你的问题,这应该有效:
var generateIcons = function(playtime, players) {
var count = playtime / 2;
icons = [];
colors = ["cyan", "violet", "orange", "black", "gold", "red", "blue", "green"];
for (var i = 0; i < count; i++) {
icons.push(colors[i % players]);
}
return icons;
};
console.log(generateIcons(60, 4));
&#13;
答案 1 :(得分:0)
检查该解决方案是否满足您:)
var generateIcons = function(playtime, players) {
var count = playtime / 2;
icons = [];
var randomNumber;
for (var i = 1; i <= count; i++) {
randomNumber = Math.floor((Math.random() * players) + 1); //generate random number from <1, players> probability for each color should be ~equal ;)
if(randomNumber == 1)
{
icons.push("green");
}
else if (randomNumber == 2)
{
icons.push("blue");
}
else if (randomNumber == 3)
{
icons.push("red");
}
else if (randomNumber == 4)
{
icons.push("gold");
}
else if (randomNumber == 5)
{
icons.push("black");
}
else if (randomNumber == 6)
{
icons.push("orange");
}
else if (randomNumber == 7)
{
icons.push("violet");
}
else
{
icons.push("cyan");
}
}
}
generateIcons(60,4);
console.log(icons);