duan246558 2017-06-05 23:48
浏览 29
已采纳

循环功能,直到大海捞针中没有指定的针

I'm trying to build a function, that will check if the needle is in haystack, if it is then the function should loop until there is none.

So far I've built a function for random number generation. What that function does is it will create a random string then hash it with sha512. The function then extracts all the numbers from a string and multiplies it by 2.

It leaves me with a long line of numbers, then I'll use mt_rand to specify start and end point for strpos. After that I just return the number that was created randomly.

function randomNumber($suurus, $max){
mb_internal_encoding("UTF-8");
$possible="aábcdeéfghiíjklmnoóöópqrstuúüűvwxyzAÁBCDEÉFGHIÍJKLMNOÓÖŐPQRSTUÚVWXYZ";
$char = mb_substr($possible,rand(0, mb_strlen($possible) - 1),1);
$str = openssl_digest($char, 'sha512');
$str = preg_replace('/[^1-9.]+/', '', $str);
$str = $str*2;
$strLen = strlen($str)-5;
$strStart =  mt_rand(1, $strLen);
$strEnd = mt_rand(1, $suurus);
$str = substr($str, $strStart, $strEnd);
while($str > $max){
  $str = $str / 2;
}
return round($str);

The thing is that this number can't repeat, I need it to be unique at all times for my app.

I actually accomplished what I wanted with if else statements, but that means I have to write a long script of repeated if statements until I get the results I want. I really don't think this is the smartest way to do things.

if(!isset($voitjad)){
                      $voitjad[$vCounter][] = array(
                        'nimi' => $voitja,
                        'auhind' => $keyCode['v'.$vCount.''],
                        'rn' => $rN,
                        'siin' => 1,
                        'id' => $voitjaID
                      );
                    }else{
                      if(!exist($rN, $voitjad)){
                        $voitjad[$vCounter][] = array(
                          'nimi' => $voitja,
                          'auhind' => $keyCode['v'.$vCount.''],
                          'rn' => $rN,
                          'siin' => 1,
                          'id' => $voitjaID
                        );
                        }else{
                          $rN = randomNumber($atLength, $atCount);
                          $voitja = $attendcheckfb['attending'][$rN]['name'];
                          $voitjaID = $attendcheckfb['attending'][$rN]['id'];
                          if(!exist($rN, $voitjad)){
                            $voitjad[$vCounter][] = array(
                              'nimi' => $voitja,
                              'auhind' => $keyCode['v'.$vCount.''],
                              'rn' => $rN,
                              'siin' => 2,
                              'id' => $voitjaID
                            );
                          }else{ and so on....

I also tried with do-while loop, but I couldn't really get it working, I'm not sure what exactly I'm doing wrong here. If you can educate me, shoot me with information.

 $nimed = array_values($nimed);
                      $voitja = $nimed[$rN]['name'];
                      $voitjaID = $nimed[$rN]['id'];
                      $voitjad[$vCounter][] = array(
                        'nimi' => $voitja,
                        'auhind' => $keyCode['v'.$vCount.''],
                        'rn' => $rN,
                        'siin' => 1,
                        'id' => $voitjaID
                      );
                      $oitjaCount++;
                      $rN = randomNumber($nimedLength, $nimedCount);
                    } while(!exist($rN, $voitjad));

If there's a way for me to get better randomly generated numbers, then please tell me, I'm open for suggestions. Meanwhile I need help with this needle, haystack thing.

  • 写回答

1条回答 默认 最新

  • ds342222 2017-06-06 00:05
    关注

    You can either do it with a loop:

    function get_unique_hashcode() {
        global $hash_array;
        do {
            $hashcode = make_random_hashcode();
        } while (in_array($hashcode, $hash_array));
        $hash_array[] = $hashcode;
        return $hashcode;
    }
    

    Or you can do it with recursion:

    function get_unique_hashcode() {
        global $hash_array;
        $hashcode = make_random_hashcode();
        if (in_array($hashcode, $hash_array)) {
            return get_unique_hashcode();
        } else {
            $hash_array[] = $hashcode;
            return $hashcode;
        }
    }
    

    This is just a general structure, you may need to adjust it for your detailed needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题