1. Did you look at the sample output? How were the color assignments NOT random? Did you even bother reading the code to understand what is happening? I added plenty of comments so that anyone analyzing the code could understand how the colors were being randomized. But, I guess I have to hold your hand.
So, after creating an array of 1 to 100 in sequential order, the next step will shuffle() the array to put all the values in a random order - but the index of the values will be from 0 to 99. So, half of the random vaues are in indexes 0 to 49 and the other half of random values are in indexes 50 to 99. Using this line:
color = ($index<50) ? 'W' : 'B';
The values in the first 50 indexes are given a color of "W", while the last 50 are given "B". BUt, they are in a random order, so the color is evenly distributed - randomly - across all the values.
2. It's not that hard, but I can see where you might have some difficulty. I could just as easily implement in the code I provided above, but decided to change the approach just for my personal interest. So, here is revised code with LOTS of comments. Be sure to look at the last line and either keep or remove that line depending upon whether you want to allow for duplicate results. I also added some debugging lines so you can see the gernerated array of possibilities. Lastly, the two variables at the top allow you to change how many possibilities and how many generated numbers are created without touching the rest of the code.
<?php
//Number of possible numbers
$maxPossible = 100;
//Number of generated attempts
$maxAttempts = 10;
//Create separate arrays for W and B (1/2 of $maxPossible each)
$white = array_fill(1, ceil($maxPossible/2), 'W');
$black = array_fill(1, floor($maxPossible/2), 'B');
//Create combined array first half 'W' and 2nd 1/2 half 'B'
$colors = array_merge($white, $black);
//Randomize the array (i.e. indexes 0 to n are randomly assigned W or B)
shuffle($colors);
//Append the colors to an array value of 0 at the 0 index
//indexes 1 to $maxPossible have randomly assinged W or B
$possibilities = array_merge(array(0), $colors);
//The followng lines are for illustrative purposes only
//Remove after you see how the array is generated
echo "<pre>";
print_r($possibilities);
echo "</pre>";
/////////////////////
for($attempt=0; $attempt<$maxAttempts; $attempt++)
{
//Generate a random number
$generatedNumber = array_rand($possibilities);
//Determine the results
if($generatedNumber==0)
{
//Value was 0
$result = '-> (value was zero)';
}
else
{
//Value was not 0, determine color and box
$color = $possibilities[$generatedNumber];
$box = ceil($generatedNumber/($maxPossible/4));
$result = "-> it displays {$color}{$generatedNumber} - Box{$box}";
}
//Output the results
echo "Generated number: {$generatedNumber} {$result}<br />\n";
//Remove generated number from possibilities (if you don't want to allow diplicates)
unset($possibleNumbers[$index]);
}
?>
hello,
I didn't meant not random like rand() function but random like not generated and more like static declared $White = array(2,4,6,8,10,11,13,15);
I've adapted the script to my needs.
Thank you very much for the time and quick response.