私は音楽オタクなので (ただし初心者のコーダーにすぎません)、このスクリプトを作成して (MySQL テーブルから) いくつかのピッチをランダムに選択しました (非公式のシリアル スタイルの作曲を支援するためです)。 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--;
}
}
}
?>