Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

random() method in php

Posted on 2006-03-24
4
Medium Priority
?
310 Views
Last Modified: 2012-05-05
hello!

i have a function in php that randomly pairs my set of instructors to my set of subjects.the number of subjects is greater than the number of instructors. how can i fairly/evenly assign subjects to instructors such that no instructor gets too many subjects??

can anyone show me a code in php using the random method that solves this problem?

thanks!
0
Comment
Question by:JaimeJegonia
  • 2
2 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16279365
You can also just shuffle the students array, and then assign them in a loop to the instrructors:


<?php

  $students = array('a','b','c','d','e','f','g','h','i','j','k');
  $instructors = array('I','II','III','IV','V');
 
  $assigned = array();
 
  $cInstructors = count($instructors);
  $cStudents    = count($students);
 
  shuffle($students);
 
  for($i = 0; $i < $cStudents; $i++) {
    $cIns = $i % $cInstructors;
    if(!isset($assigned[$cIns])) $assigned[$cIns] = array();
    $assigned[$cIns][] = $students[$i];
  }
 
  echo '<pre>';
  print_r($assigned);
  echo '</pre>';
 
?>

The $assigned array, will hold $instructorId => array($student, $student, $student) key-value pairs.

-r-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16279375
Sorry, the forloop didn't retrieve the actual instructor Id. Use this instead:

<?php

  $students = array('a','b','c','d','e','f','g','h','i','j','k');
  $instructors = array('I','II','III','IV','V');
 
  $assigned = array();
 
  $cInstructors = count($instructors);
  $cStudents    = count($students);
 
  shuffle($students);
 
  for($i = 0; $i < $cStudents; $i++) {
    $kIns = $instructors[$i % $cInstructors];
    if(!isset($assigned[$kIns])) $assigned[$kIns] = array();
    $assigned[$kIns][] = $students[$i];
  }
 
  echo '<pre>';
  print_r($assigned);
  echo '</pre>';
 
?>
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question