Canman2005 Posted August 31, 2009 Share Posted August 31, 2009 Hi all I have the following if(getramdoms(2) != 0) { $rr = $rr. '1,'; } if(getramdoms(44) != 0) { $rr = $rr. '2,'; } if(getramdoms(99) != 0) { $rr = $rr. '3,'; } if(getramdoms(132) != 0) { $rr = $rr. '4,'; } print $rr; which produces something like 1,3,4, My question is, how can I randomly pick one of those values and set the random value as $randomnumber = ? any help would be great as im totally stumped thanks very much for any help Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/ Share on other sites More sharing options...
Canman2005 Posted August 31, 2009 Author Share Posted August 31, 2009 can I not do something like rand($rr)? any help would be great Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909459 Share on other sites More sharing options...
Canman2005 Posted August 31, 2009 Author Share Posted August 31, 2009 I have tried using $exploded = explode(",", "$rr"); $rand = rand(1, count($exploded)); echo $exploded[$rand]; which kinda works, it displays a random number, but sometimes returns nothing (although it could be 0 it's returning) any ideas why it would do that? Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909461 Share on other sites More sharing options...
Mark Baker Posted August 31, 2009 Share Posted August 31, 2009 $exploded = explode(",", "$rr"); $rand = rand(0, count($exploded)-1); echo $exploded[$rand]; Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909583 Share on other sites More sharing options...
Canman2005 Posted August 31, 2009 Author Share Posted August 31, 2009 I still get 0 returned as a random number, the code used now is $rr = '1,2,3,4,5,'; $exploded = explode(",", "$rr"); $rand = rand(0, count($exploded)-1); print $rand; any ideas? thanks Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909686 Share on other sites More sharing options...
Psycho Posted August 31, 2009 Share Posted August 31, 2009 You need to redo how you are creating the values. By concatentating the values into single string with a comma at the end, you are making this more difficult than it needs to be. If you explode the values like Mark suggests you will alwaus have an empty value at the end of the array. Create your values in an array to begin with and then just use array_rand() to get a random value out of that array $rr = array(); if(getramdoms(2) != 0) { $rr[] = 1; } if(getramdoms(44) != 0) { $rr[] = 2; } if(getramdoms(99) != 0) { $rr[] = 3; } if(getramdoms(132) != 0) { $rr[] = 4; } $randomnumber = array_rand($rr); Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909690 Share on other sites More sharing options...
Canman2005 Posted August 31, 2009 Author Share Posted August 31, 2009 Okay, but if I do $rr = array(); $rr[] = 2; $rr[] = 4; $randomnumber = array_rand($rr); print $randomnumber; it outputs only 0 and 1 where it should be outputting just 2 and 4 any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909723 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 when picking just one entry with array rand, it returns the key of the array, not the value at that key, so the last line to print $rr[$randomnumber]; Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909729 Share on other sites More sharing options...
Canman2005 Posted August 31, 2009 Author Share Posted August 31, 2009 top job thanks Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909759 Share on other sites More sharing options...
Psycho Posted August 31, 2009 Share Posted August 31, 2009 when picking just one entry with array rand, it returns the key of the array, not the value at that key, so the last line to print $rr[$randomnumber]; Yeah, I missed adding that - thanks for the assist. Could also do this $randomnumber = $rr[array_rand($rr)]; Quote Link to comment https://forums.phpfreaks.com/topic/172513-solved-picking-random/#findComment-909823 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.