dsxml2169 2011-04-02 18:49 采纳率: 0%
浏览 42

根据成绩从数据库中随机组成学生

I have table with 30 students with grades from 1 to 5. How to make 6 different students group which contains five randomly selected students? Each group must have five students and each of those five students must have different grade.

First group: student1 (grade1), student2 (grade2), student3 (grade3), student4 (grade4), student5 (grade5)

  • 写回答

1条回答 默认 最新

  • doutuo2829 2011-04-02 19:37
    关注

    Pull in the data from the table, structure the results to group by grade, so you have array('Grade1' => array('Student1','Student2','etc.'), 'Grade2' => array('Student3','Student4','etc)) and so forth. Randomize the sub arrays like:

    foreach($students as $key=>$student)
    {
        shuffle($students[$key]);
    }
    

    Then you can just go across the arrays so $group[0] would be = to array($students['Grade1'][0],$students['Grade2'][0],$students['Grade3'][0],etc.) which could be done with a for() loop.

    You could do most of the work with randomizing and limit your results in the SQL query by doing something like:

    "SELECT * FROM `Students` WHERE `Grade` = 'Grade1' ORDER BY RAND() LIMIT 6"
    

    to get 6 randomly ordered results from the table for that grade to begin with. Of course, that would require a separate SQL query for each of the grades. There's probably a way to get 6 random results for each grade in a single query.

    I'm also assuming that the same student cannot be in two different groups and there are the same students in each group. If they're uneven, and they can be in more than one group, then you'd have to randomly select one of the students from the grade to fill the spot, which could easily done with something like $students['GradeX'][mt_rand(0,count($students['GradeX']) - 1)]

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测