Jay2391 Posted January 30, 2007 Share Posted January 30, 2007 I am trying to store randon numbers in variable without haven't any duplicatesI did this ... I just want to know if there is a easier way because i what to know what to do when i get to 32 teams...I mean this works just a lot of code ::)[code]<?php$teams = 4;if ($teams == 4){ $random1 = rand (1,$teams); $random2 = rand (1,$teams); $random3 = rand (1,$teams); $random4 = rand (1,$teams); while($random1 == $random2 or $random1 == $random3 or $random1 == $random4 or $random2 == $random3 or $random2 == $random4 or $random3 == $random4){ $random2 = rand (1,$teams); $random3 = rand (1,$teams); $random4 = rand (1,$teams); } echo "$random1<br>"; echo "$random2<br>"; echo "$random3<br>"; echo "$random4<br>";}?>[/code] Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted January 30, 2007 Share Posted January 30, 2007 i'm not sure what you're trying to do here. maybe this is what you want:[code]<?php $teams = 4; $input = rand(1, $teams); $rand_array = array(); for($i = 0; $i < $teams; $i++){ foreach($rand_array as $key => $val){ (($input != $val) ? ($bool = false) : ($bool = true)); } (($bool == false) ? ($rand_array[] = $input)); } print_r($rand_array);?>[/code] Quote Link to comment Share on other sites More sharing options...
ninja Posted January 30, 2007 Share Posted January 30, 2007 so..are you just wanting like, the numbers 1 through 32 in random order?[code]for ($num = 1; $num < 33; $num++) { $number[] = $num;}shuffle($number);[/code] Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted January 30, 2007 Share Posted January 30, 2007 [quote author=ninja link=topic=124729.msg517235#msg517235 date=1170180101]so..are you just wanting like, the numbers 1 through 32 in random order?[code]for ($num = 1; $num < 33; $num++) { $number[] = $num;}shuffle($number);[/code][/quote]i think it may have something to do with the value of $teams. at first i thought exactly what you thought. but then i saw that $teams = 4. and then he has four different $random variables... i figured maybe they're related. not sure tho. Quote Link to comment Share on other sites More sharing options...
ninja Posted January 30, 2007 Share Posted January 30, 2007 well it looks like he was just going about a really complicated way of ordering 4 numbers in random order, and even though it "worked" he thought that it would be a lot more work if he had 32 teams. meh, idk. Quote Link to comment Share on other sites More sharing options...
Jay2391 Posted January 30, 2007 Author Share Posted January 30, 2007 the numbers need to be store in separate variables so they can be assigned...like userx will be rank 4userb will be rank 2but they can't be duplicated that is why the number need to be store in a unique variable Quote Link to comment Share on other sites More sharing options...
ninja Posted January 31, 2007 Share Posted January 31, 2007 well the $number array I made is a well..it's an array. It's like having 42 unique variables, only since it's an array, it's easy to do things with those 42 unique variables, like loop through them, shuffle them around, not have to write out 42 unique variables, etc.. You can't seriously be wanting to write out 42 individual variables, are you? Especially if they are all pretty much going to represent the same thing, only different "teams" or whatever. Quote Link to comment Share on other sites More sharing options...
btherl Posted January 31, 2007 Share Posted January 31, 2007 Jay2391, I suggest you do some tutorials on arrays. They are very useful! And they are ideal for what you want to do here. Without them, your programming will be limited to very simple things only. Quote Link to comment Share on other sites More sharing options...
Jay2391 Posted February 4, 2007 Author Share Posted February 4, 2007 hey guys I was trying a few of this ideas but they didn't work anyone else know what to do ???and make it simple i have a pres et number X belive is 4I need 1 thru 4 randon and no duplicates in a variable.. I did it on top but it get complicated when that number changes Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 4, 2007 Share Posted February 4, 2007 This question is very similar to another that was posted just a few days ago. Here's the modification of my answer:[code]<?php$ok_rand = array();$max = 4;for ($i=0;$i<$max;$i++) { $test_rand = rand(1,$max); // get a random number while(in_array($test_rand,$ok_rand)) $test_rand = rand(1,$max); // have we seen this number already? Yes -- generate another $ok_rand[$i] = $test_rand; // store unique number}$unq = array_unique($ok_rand);$dif = array_diff($ok_rand,$unq);if (empty($dif)) echo "All generated random numbers are unique";echo '<pre>' . print_r($ok_rand,true) . '</pre>';?>[/code]Just change $max to get more numbers.Ken Quote Link to comment Share on other sites More sharing options...
Jay2391 Posted February 4, 2007 Author Share Posted February 4, 2007 I get this ...All generated random numbers are uniqueArray( [0] => 1 [1] => 4 [2] => 2 [3] => 3)which is great but i need to attach a name to that number so i need it to print like 1 then i attach the name 2 attach another name ...how do i print that Quote Link to comment Share on other sites More sharing options...
Jay2391 Posted February 4, 2007 Author Share Posted February 4, 2007 Thanks got it<?php$ok_rand = array();$max = 10;for ($i=0;$i<$max;$i++) { $test_rand = rand(1,$max); // get a random number while(in_array($test_rand,$ok_rand)) $test_rand = rand(1,$max); // have we seen this number already? Yes -- generate another $ok_rand[$i] = $test_rand; // store unique number echo "$test_rand<br>";}?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 4, 2007 Share Posted February 4, 2007 Since all the numbers are in an array, just use the arrray references instead of individual variables.Ken 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.