【问题标题】:Php/MySQL random data (musical pitch) sequencesPhp/MySQL 随机数据(音高)序列
【发布时间】:2013-06-05 11:26:08
【问题描述】:

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


?>

【问题讨论】:

    标签: php mysql random


    【解决方案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;
    

    【讨论】:

    • 谢谢!我曾认为也许一个数组会是解决方案。我试试看。
    • 没问题 =] 顺便说一句,我现在已经测试过了,似乎对我来说工作正常
    • 天哪,它直接从 ctrl-v 中完美运行。 (现在我只需要弄清楚为什么!)您,好心的先生或女士,是我的新英雄!
    【解决方案2】:

    相信我,我见过更糟糕的代码,但如果您熟悉 oop 编码,您可以使用函数来获取 $numtot 的值并返回适当的结果,或者考虑策略设计模式。

    你们教吉他吗? :)

    【讨论】:

    • 谢谢,你能给我一个这样的函数的例子sn-p吗?我会用你来换取教训!
    • 问题是 if-else 路径中的每个条件都给出了一组略有不同的 html。所以我不确定这将如何转化为函数。
    • 是的,我会选择 Pudge601,但我会忘记课程
    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2011-09-18
    • 2021-12-23
    相关资源
    最近更新 更多