因为我是一个音乐书呆子(但只是一个初级编码员),所以我制作了这个脚本来随机选择一些音高(从 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--;
}
}
}
?>