Php / MySQL随机数据(音高)序列

时间:2013-06-05 11:26:09

标签: php mysql random

因为我是一个音乐书呆子(但只是一个开始编码器),我让这个脚本随机选择一些音高(从MySQL表中)(以帮助非正式的连续剧组合。)它给出9-12不重复音高名称分为3-4组,每组3-4个音高。例如: C,Eb,Gb(断裂)A,D,F,Db(断裂)Ab,G,B。

1)是否有更有效的方法来实现这一预期结果?代码似乎很麻烦。我只是通过一堆if-else语句。 2)正如代码开头的注释所示,各种分布的概率是不相等的。在实践中,这很好但是有一个替代方案会很好。

感谢。

<?php

/*
This code assigns pitch distributions to one of the following combinations. 
The first number is possible values of $numtot:
9 = 3,3,3
10 = 4,3,3; 
     3,4,3; 3,3,4
11 = 4,4,3; 4,3,4; 
     3,4,4; 
12 = 4,4,4; 
     3,3,3,3;
Note that probabilities are unequal:
eg: 3,3,3 is 2x as likely to occur as 3,3,3,3
*/

$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);

$numtot = rand(9,12);

$sql = "SELECT * FROM pitches ORDER BY RAND() LIMIT $numtot";
$result = mysql_query($sql, $sandbox);

echo "Your pitches are:<br>";

if ($numtot==9) {
    while ($row = mysql_fetch_array ($result)) {
    if ($numtot>7)
        {echo $row['Pitch'].", ";}
    else if ($numtot==7)
        {echo $row['Pitch']."<br>";}
    else if ($numtot>4) 
        {echo $row['Pitch'].", ";}
    else if ($numtot==4) 
        {echo $row['Pitch']."<br> ";}
    else if ($numtot>1)
        {echo $row['Pitch'].", ";}  
    else
        {echo $row['Pitch'];}
    $numtot--;
    }
}

else if ($numtot==10) {
    $num1 = rand(3,4);
    if ($num1 == 4){
        while ($row = mysql_fetch_array ($result)) {
            if ($numtot>7)
                {echo $row['Pitch'].", ";}
            else if ($numtot==7)
                {echo $row['Pitch']."<br>";}
            else if ($numtot>4) 
                {echo $row['Pitch'].", ";}
            else if ($numtot==4) 
                {echo $row['Pitch']."<br> ";}
            else if ($numtot>1)
                {echo $row['Pitch'].", ";}  
            else
                {echo $row['Pitch'];}
        $numtot--;
        }
    }
    else {
        $num2 = rand(3,4);
        if ($num2 == 4) {
            while ($row = mysql_fetch_array ($result)) {
                if ($numtot>8)
                    {echo $row['Pitch'].", ";}
                else if ($numtot==8)
                    {echo $row['Pitch']."<br>";}
                else if ($numtot>4) 
                    {echo $row['Pitch'].", ";}
                else if ($numtot==4) 
                    {echo $row['Pitch']."<br> ";}
                else if ($numtot>1)
                    {echo $row['Pitch'].", ";}  
                else
                    {echo $row['Pitch'];}
            $numtot--;
            }
        }
        else {
            while ($row = mysql_fetch_array ($result)) {
                if ($numtot>8)
                    {echo $row['Pitch'].", ";}
                else if ($numtot==8)
                    {echo $row['Pitch']."<br>";}
                else if ($numtot>5) 
                    {echo $row['Pitch'].", ";}
                else if ($numtot==5) 
                    {echo $row['Pitch']."<br> ";}
                else if ($numtot>1)
                    {echo $row['Pitch'].", ";}  
                else
                    {echo $row['Pitch'];}
            $numtot--;
            }
        }
    }
}

else if ($numtot==11) {
    $num1 = rand(3,4);
    if ($num1 == 4){
        while ($row = mysql_fetch_array ($result)) {
            if ($numtot>8)
                {echo $row['Pitch'].", ";}
            else if ($numtot==8)
                {echo $row['Pitch']."<br>";}
            else if ($numtot>4) 
                {echo $row['Pitch'].", ";}
            else if ($numtot==4) 
                {echo $row['Pitch']."<br> ";}
            else if ($numtot>1)
                {echo $row['Pitch'].", ";}  
            else
                {echo $row['Pitch'];}
        $numtot--;
        }
    }
    else {
        $num2 = rand(3,4);
        if ($num2 == 4) {
            while ($row = mysql_fetch_array ($result)) {
                if ($numtot>8)
                    {echo $row['Pitch'].", ";}
                else if ($numtot==8)
                    {echo $row['Pitch']."<br>";}
                else if ($numtot>5) 
                    {echo $row['Pitch'].", ";}
                else if ($numtot==5) 
                    {echo $row['Pitch']."<br> ";}
                else if ($numtot>1)
                    {echo $row['Pitch'].", ";}  
                else
                    {echo $row['Pitch'];}
            $numtot--;
            }
        }
        else {
            while ($row = mysql_fetch_array ($result)) {
                if ($numtot>9)
                    {echo $row['Pitch'].", ";}
                else if ($numtot==9)
                    {echo $row['Pitch']."<br>";}
                else if ($numtot>5) 
                    {echo $row['Pitch'].", ";}
                else if ($numtot==5) 
                    {echo $row['Pitch']."<br> ";}
                else if ($numtot>1)
                    {echo $row['Pitch'].", ";}  
                else
                    {echo $row['Pitch'];}
            $numtot--;
            }
        }
    }
}

else if ($numtot==12) {
    $num1 = rand(3,4);
    if ($num1 == 4){
        while ($row = mysql_fetch_array ($result)) {
            if ($numtot>9)
                {echo $row['Pitch'].", ";}
            else if ($numtot==9)
                {echo $row['Pitch']."<br>";}
            else if ($numtot>5) 
                {echo $row['Pitch'].", ";}
            else if ($numtot==5) 
                {echo $row['Pitch']."<br> ";}
            else if ($numtot>1)
                {echo $row['Pitch'].", ";}  
            else
                {echo $row['Pitch'];}
        $numtot--;
        }
    }
    else {
        while ($row = mysql_fetch_array ($result)) {
            if ($numtot>10)
                {echo $row['Pitch'].", ";}
            else if ($numtot==10)
                {echo $row['Pitch']."<br>";}
            else if ($numtot>7) 
                {echo $row['Pitch'].", ";}
            else if ($numtot==7) 
                {echo $row['Pitch']."<br> ";}
            else if ($numtot>4) 
                {echo $row['Pitch'].", ";}
            else if ($numtot==4) 
                {echo $row['Pitch']."<br> ";}                       
            else if ($numtot>1)
                {echo $row['Pitch'].", ";}  
            else
                {echo $row['Pitch'];}
            $numtot--;
        }
    }
}


?>

2 个答案:

答案 0 :(得分:1)

由于可能的分布是有限的,已知的,而不是很多,您可以创建它们的哈希查找,您可以从中随机选择一个。此代码未经测试,但原则上应该有效:

$dists = array(
    array(array(3,3,3)),
    array(array(4,3,3),array(3,4,3),array(3,3,4)),
    array(array(4,4,3),array(4,3,4),array(3,4,4)),
    array(array(4,4,4),array(3,3,3,3))
);

$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);

$numtot = rand(9,12);

$sql = "SELECT * FROM pitches ORDER BY RAND() LIMIT $numtot";
$result = mysql_query($sql, $sandbox);

$index = $numtot - 9;
$dist = $dists[$index][array_rand($dists[$index])];

$i = 1;
$j = 0;
$out = '';
while ($row = mysql_fetch_array ($result)) {
    $out .= $row['Pitch'];
    if ($i == $dist[$j]) {
        $out .= '<br>';
        $i = 1;
        $j++;
    } else {
        $out .= ',';
        $i++;
    }
}
echo $out;

答案 1 :(得分:0)

相信我,我已经看到了更糟糕的代码,但你可以使用函数来清理它,以获取$ numtot的值并返回相应的结果,或者如果你熟悉oop编码,则考虑策略设计模式。

你上吉他课吗? :)