cromulent Posted August 22, 2013 Share Posted August 22, 2013 I would like to know how I can create a customized array_rand so I can give it a seed and generate expected results based on the number seeded. Any thoughts on how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/ Share on other sites More sharing options...
.josh Posted August 22, 2013 Share Posted August 22, 2013 why would you want to get "expected results" out of something that is supposed to return a randomly selected entry? If you want to be able to pass an argument and receive a specific result, it's no longer random thing. Perhaps you aren't explaining it right, or perhaps I am not understanding it right? In any case, since you are trying to control or otherwise influence the random selection, instead of using the built in functions that shuffle or return a random entry, you'd use the functions that return a specifically specified entry, and then just write your own "randomize" logic, around whatever you pass to the function. So IOW you'd be using things like count or array_keys to get info about the array. Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446328 Share on other sites More sharing options...
cromulent Posted August 22, 2013 Author Share Posted August 22, 2013 The results of my custom_array_rand will be based off answers a user gives(the seed) to a set of questions. If they answer the questions the same I want the results to be the same although I don't want to go through myself and assign the results for each possible set of answers. Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446334 Share on other sites More sharing options...
AbraCadaver Posted August 22, 2013 Share Posted August 22, 2013 Unless it is more complicated than you state. I can give a custom_array_rand(), but why is it needed? $answer = 2; $array = array(1=>'something one', 2=>'something two', 3=>'something three'); $result = $array[$answer]; Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446336 Share on other sites More sharing options...
.josh Posted August 22, 2013 Share Posted August 22, 2013 Well it sounds like you're going to have to do some mapping of answer combinations to array entries, regardless. I don't think there's really any way around that, though you could automate the mapping.. for example, you can generate the possible combinations to answers easy enough with a script, though I suppose the list and time it takes would grow exponentially based on how many questions and answers there are.. but in any case, you could then hash each combination (again, you can automate this..just throw this into the script that generates the answer combinations) and then use the hashes as the array keys to your array. Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446338 Share on other sites More sharing options...
cromulent Posted August 22, 2013 Author Share Posted August 22, 2013 I probably did not do a very good job of explaining what I wanted but I think I came up with a solution using array_multisort: $arr = array('a','quick','brown','fox','jumped','over','the','lazy','dog'); $numbers = range(1, sizeof($arr)); $myseed = 20; //generated from the results of a users questions srand($myseed); shuffle($numbers); array_multisort($numbers, $arr); foreach ($arr as $value) { echo "Value: $value<br />\n"; } At this point I will just need a subset of the result array. Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446347 Share on other sites More sharing options...
AbraCadaver Posted August 22, 2013 Share Posted August 22, 2013 (edited) 1. Why not just shuffle() $arr instead of sorting based on your shuffled array? 2. What conditions do you have as to how to get the subset? array_slice()? function custom_array_rand($array, $seed, $num) { srand($seed); shuffle($array); srand(); //reset so other functions don't use this same seed return array_slice($array, 0, $num); } Needs error/arg checking. Edited August 22, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446352 Share on other sites More sharing options...
Solution kicken Posted August 22, 2013 Solution Share Posted August 22, 2013 All you need to do is call srand with a value before your array_rand call. That will seed the generator PHP uses and cause array_rand to return predictable results. <?php $range = range('a','z'); if (isset($_GET['seed'])) srand($_GET['seed']); var_dump(array_rand($range, 5)); Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446357 Share on other sites More sharing options...
cromulent Posted August 22, 2013 Author Share Posted August 22, 2013 That is what I was looking for. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281472-array_rand-with-a-seed/#findComment-1446364 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.