Jump to content

array_rand with a seed


cromulent
Go to solution Solved by kicken,

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by AbraCadaver
Link to comment
Share on other sites

  • Solution

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));
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.