MuRD3ReR Posted August 5, 2011 Share Posted August 5, 2011 How can i get different random numbers ? For example i need 5 numbers it will pick from 0 to 5 and the 5 numbers won't equal with others and i want to be with the FOR loop. Exapmle i want something like this ; for($ix=0;$ix < 5;$ix++) { $rand_number = rand(0, 5); echo $rand_number; } I want the numbers to be different from others. Link to comment https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/ Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 You would need to use RANGE in this case... similiar to Python Lists <?php $min =0; $max = 5; $rand = range($min, $max); $number = array(); shuffle($rand); for($i=0;$i<=count($rand);$i++){ $number = $rand; } print_r($number); ?> Array ( [0] => 0 [1] => 3 [2] => 4 [3] => 2 [4] => 1 [5] => 5 ) Link to comment https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/#findComment-1252879 Share on other sites More sharing options...
MuRD3ReR Posted August 6, 2011 Author Share Posted August 6, 2011 You would need to use RANGE in this case... similiar to Python Lists <?php $min =0; $max = 5; $rand = range($min, $max); $number = array(); shuffle($rand); for($i=0;$i<=count($rand);$i++){ $number = $rand; } print_r($number); ?> Array ( [0] => 0 [1] => 3 [2] => 4 [3] => 2 [4] => 1 [5] => 5 ) Thanks for your support. I didn't try this code but i did what i want with your previous code maybe this will work too but i didn't tried Thank again Link to comment https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/#findComment-1252960 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 No second the code, my first code I misunderstood what you wanted.. The second code will do exactly what you want. No Problem. Link to comment https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/#findComment-1252964 Share on other sites More sharing options...
MuRD3ReR Posted August 6, 2011 Author Share Posted August 6, 2011 No second the code, my first code I misunderstood what you wanted.. The second code will do exactly what you want. No Problem. Works great and your second code Link to comment https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/#findComment-1252971 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 Np, hope your still not using the first code, because it WON'T generate the amount of numbers you want specifically. It will populate it with 5 numbers, however if 2 of those 5 numbers are the same it will take one of them away. What you want is a range of 5 total digits with different numbers.. so Again, use the second code if you havn't yet, and its my pleasure to help. Mark the Topic As Solved Link to comment https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/#findComment-1252973 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.