• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array Randomizer?

#1
Wink 
Hi everyone.

I want to distribute/assign some data randomly but even amount from an array of users to an array of data.

Here's an example:

I have an array of user ids:
PHP Code:
$users = array(204725210194); 

Then the array of data:
PHP Code:
$data = array(
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
 
   array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???'),
    array('department' => '???''datecreated' => '???')
);

// Just assume that we have real data :) 

And the result should be like this (to be used by Query Builder's insert_batch()):
PHP Code:
$data = array(
 
   array('department' => '???''datecreated' => '???''assignee' => '20'),
 
   array('department' => '???''datecreated' => '???''assignee' => '210'),
 
   array('department' => '???''datecreated' => '???''assignee' => '25'),
 
   array('department' => '???''datecreated' => '???''assignee' => '194'),
 
   array('department' => '???''datecreated' => '???''assignee' => '47'),
 
   array('department' => '???''datecreated' => '???''assignee' => '20'),
 
   array('department' => '???''datecreated' => '???''assignee' => '25'),
 
   array('department' => '???''datecreated' => '???''assignee' => '194'),
    array('department' => '???''datecreated' => '???''assignee' => '20'),
    array('department' => '???''datecreated' => '???''assignee' => '194'),
    array('department' => '???''datecreated' => '???''assignee' => '210'),
    array('department' => '???''datecreated' => '???''assignee' => '47'),
    array('department' => '???''datecreated' => '???''assignee' => '210'),
    array('department' => '???''datecreated' => '???''assignee' => '25'),
    array('department' => '???''datecreated' => '???''assignee' => '20'),
    array('department' => '???''datecreated' => '???''assignee' => '210'),
    array('department' => '???''datecreated' => '???''assignee' => '47')
); 

Now, I want to assign (from the example) 17 rows to 5 users. So 3 for each user (and the remaining 2 will be either distributed to a random user or to the last person). We want this to be random. I tried to code it myself but failed.

How can I achieve this? Any help?
Thank you very much Smile
Reply

#2
You could use Tetris next block method:

PHP Code:
// list of available user options
$users = [204725210194];

// define temp variable
$availableUsers = [];

foreach (
$data as $i => $row) {
    
// if there are no unique options available, reset available options temp array
    
if (!$availableUsers ) {
        
$availableUsers $users;
    }

    
// pick and assign random user
    
$randomUser array_rand($availableUsers);
    
$data[$i]['assignee'] = $availableUsers[$randomUser];

    
// remove option from available list
    
unset($availableUsers[$randomUser]);

Reply

#3
(12-12-2018, 05:07 AM)Pertti Wrote: You could use Tetris next block method:

PHP Code:
// list of available user options
$users = [204725210194];

// define temp variable
$availableUsers = [];

foreach (
$data as $i => $row) {
 
   // if there are no unique options available, reset available options temp array
 
   if (!$availableUsers ) {
 
       $availableUsers $users;
 
   }

 
   // pick and assign random user
 
   $randomUser array_rand($availableUsers);
 
   $data[$i]['assignee'] = $availableUsers[$randomUser];

 
   // remove option from available list
 
   unset($availableUsers[$randomUser]);


Wow. You saved my life. Thank you so much. I checked the results and your answer fulfilled my expectation.
Again, thank you. +1 rep for you.

(and sorry for the bad grammar..)
Reply

#4
Happy to help Smile

I was working on Tetris related side project for game jam entry, and remembered reading on how they make sure they have even spread on random blocks, seemed like could solve your problem.

And cheers for the rep, always good to know you're doing something right Big Grin
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.