Php/MySQL random data (musical pitch) sequences

2019-09-08 09:00发布

Because I am a music nerd (but only a beginning coder), I made this script to chose some pitches (from a MySQL table) at random (to help with informal serial-style composition.) It gives 9-12 nonrepeating pitch names in 3-4 sets of 3-4 pitches each. For example: C, Eb, Gb (break) A, D, F, Db (break) Ab, G, B.

1) Is there a more efficient way to achieve this desired result? The code seems cumbersome. I just put it through a bunch of if-else statements. 2) As the comment at the beginning of the code shows, the probability of the various distributions is unequal. In practice that's fine but it would be nice to have an alternative.

Thanks.

<?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--;
        }
    }
}


?>

标签: php mysql random
2条回答
欢心
2楼-- · 2019-09-08 09:09

Since the possible distributions are finite, known, and not that many, you can create a hash lookup of them, from which you can choose one at random. This code is untested, but should work in principle:

$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;
查看更多
劳资没心,怎么记你
3楼-- · 2019-09-08 09:21

Trust me I've seen worse code but you could clean this up using a function to take the value of $numtot and return the appropriate result or consider the strategy design pattern if you are familiar with oop coding.

Do you give guitar lessons? :)

查看更多
登录 后发表回答