blacksharkmedia Posted June 14, 2010 Share Posted June 14, 2010 Hello, I wanna make an application to run some simulations: 1. I have numbers from 0-100 2. 50 numbers are White, and 50 numbers are Black, 0 does not have any color The numbers are assigned by default in config to a color code ex: W: 1,4,6,34,65... B:2,3,11,... So what is found in color code W you won't find in color code B. 3. The 100 numbers are divided in 4 equal boxes ex: box1: 1-25, box2: 26-50, etc.. in every box is present also the W and the B color codes. 4. So the problem is that I want for every random(from 0-100) number generated to be automaticaly color coded using the default config color codes(w or b) and to be assigned to one of the 4 groups. If the number 0 is generated to display a message. Example W(white): 1,3,5,7,9,12,... B(black): 6,8,11,67,44,... Box1: 1-25 Box2: 26-50 Box3: 51-75 Box4: 75-100 Generated number: 7 -> it displays W7 - box1 Generated number: 67 -> it displays B67 - Box3 The number of random generations to be config by default to 10 Any ideas on how this can be implemented using php/mysql/arrays/etc..? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted June 15, 2010 Share Posted June 15, 2010 I'm really not sure what your asking. You've written a rough spec, all you need to do is translate that into code. If your stuck there, you'll likely need to post your request looking for a programmer within the freelance board. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2010 Share Posted June 15, 2010 Pretty simple I think: <?php //genrate array of number 1 to 100 $numbers = range(1, 100); //Randomize the array shuffle($numbers); //generate 10 random numbers and output the results for($rand=0; $rand<10; $rand++) { $index = array_rand($numbers); $number = $numbers[$index]; //Remove value from array to prevent duplicates unset($numbers[$index]); $color = ($index<50) ? 'W' : 'B'; $box = "Box" . ceil($number/25); echo "Generated number: {$number} -> it displays {$color}{$number} - {$box}<br />\n"; } ?> Example output: Generated number: 14 -> it displays W14 - Box1 Generated number: 4 -> it displays W4 - Box1 Generated number: 72 -> it displays B72 - Box3 Generated number: 73 -> it displays W73 - Box3 Generated number: 43 -> it displays W43 - Box2 Generated number: 20 -> it displays B20 - Box1 Generated number: 54 -> it displays W54 - Box3 Generated number: 99 -> it displays B99 - Box4 Generated number: 90 -> it displays W90 - Box4 Generated number: 88 -> it displays B88 - Box4 Quote Link to comment Share on other sites More sharing options...
blacksharkmedia Posted June 15, 2010 Author Share Posted June 15, 2010 Pretty simple I think: <?php //genrate array of number 1 to 100 $numbers = range(1, 100); //Randomize the array shuffle($numbers); //generate 10 random numbers and output the results for($rand=0; $rand<10; $rand++) { $index = array_rand($numbers); $number = $numbers[$index]; //Remove value from array to prevent duplicates unset($numbers[$index]); $color = ($index<50) ? 'W' : 'B'; $box = "Box" . ceil($number/25); echo "Generated number: {$number} -> it displays {$color}{$number} - {$box}<br />\n"; } ?> Example output: Generated number: 14 -> it displays W14 - Box1 Generated number: 4 -> it displays W4 - Box1 Generated number: 72 -> it displays B72 - Box3 Generated number: 73 -> it displays W73 - Box3 Generated number: 43 -> it displays W43 - Box2 Generated number: 20 -> it displays B20 - Box1 Generated number: 54 -> it displays W54 - Box3 Generated number: 99 -> it displays B99 - Box4 Generated number: 90 -> it displays W90 - Box4 Generated number: 88 -> it displays B88 - Box4 Many thans for the ideea. It works fine. Also I have some tweeks for it: 1. The color codes are not secvential! From the total of 100 numbers 50 are chosen manualy to be W and 50 B and declared at simulation start. 2. 0 should be introduced to simulation, if shows up in random function then display a message like "0 was find" Thanks again Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2010 Share Posted June 15, 2010 Many thans for the ideea. It works fine. Also I have some tweeks for it: 1. The color codes are not secvential! From the total of 100 numbers 50 are chosen manualy to be W and 50 B and declared at simulation start. 2. 0 should be introduced to simulation, if shows up in random function then display a message like "0 was find" 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]); } ?> Quote Link to comment Share on other sites More sharing options...
blacksharkmedia Posted June 15, 2010 Author Share Posted June 15, 2010 Many thans for the ideea. It works fine. Also I have some tweeks for it: 1. The color codes are not secvential! From the total of 100 numbers 50 are chosen manualy to be W and 50 B and declared at simulation start. 2. 0 should be introduced to simulation, if shows up in random function then display a message like "0 was find" 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.