首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使存在唯一的检查,唯一的字符串生成器也会生成重复的字符串。

问即使存在唯一的检查,唯一的字符串生成器也会生成重复的字符串。
EN

Stack Overflow用户
提问于 2015-11-12 11:27:33
回答 1查看 67关注 0票数 0

我正在创建一个小类,它使用gfycat单词列表来生成唯一的字符串。

代码语言:javascript
复制
<?php

namespace Jamosaur\Randstring;

class Randstring
{
    private $adjectives;
    private $animals;

    private $min;
    private $max;
    private $case;
    private $maxLength;

    private $string;

    public $combinations = [];

    private $first;
    private $second;

    public $adjective;
    public $animal;
    public $number;

    /**
     * Randstring constructor.
     * @param null $case (ucwords, ucfirst)
     * @param int $maxLength
     * @param int $min
     * @param int $max
     */
    public function __construct($case = null, $maxLength = 100, $min = 1, $max = 99)
    {
        $this->case         = $case;
        $this->maxLength    = $maxLength;
        $this->min          = $min;
        $this->max          = $max;
        $this->adjectives   = explode(PHP_EOL, file_get_contents(__DIR__.'/dictionaries/adjectives.txt'));
        $this->animals      = explode(PHP_EOL, file_get_contents(__DIR__.'/dictionaries/animals.txt'));
    }

    /**
     * @param null $first
     * @param null $second
     */
    public function generateNumbers($first = null, $second = null)
    {
        $this->first    = ($first) ? $first : mt_rand(0, count($this->adjectives) - 1);
        $this->second   = ($second) ? $second : mt_rand(0, count($this->animals) - 1);
        $this->number   = mt_rand($this->min, $this->max);
        if (isset($this->combinations[$this->first.'.'.$this->second.$this->number])) {
            $this->generateNumbers($this->first, $this->second);
        }
        $this->combinations[$this->first.'.'.$this->second.$this->number] = 1;
}

    /**
     * Generate a string.
     */
    public function generateString()
    {
        $this->generateNumbers();
        $this->adjective    = $this->adjectives[$this->first];
        $this->animal       = $this->animals[$this->second];
        switch ($this->case) {
            case 'ucfirst':
                $this->string   = ucfirst($this->adjective.$this->animal.$this->number);
                break;
            case 'ucwords':
            $this->string   = ucfirst($this->adjective).ucfirst($this->animal).ucfirst($this->number);
                break;
            default:
                $this->string   = $this->adjective.$this->animal.$this->number;
                break;
        }
    }

    /**
     * @return mixed
     */
    public function generate()
    {
        $this->generateString();
        if (strlen($this->string) > $this->maxLength) {
            return $this->generate();
        }

        return $this->string;
    }
}

我添加了一个签入来记录在generateNumbers()中创建的每个组合,它应该将每个组合存储在一个数组中。

我已经设置了一个小测试来生成10000个唯一的字符串,只用于性能测试,这是用下面的代码片段完成的:

代码语言:javascript
复制
$rand = new Jamosaur\Randstring\Randstring(null, 10);
for ($i=0; $i < 10000; $i++) {
    $t[$i] = $rand->generate();
}
echo 'Unique Strings: '.count(array_unique($t)).'<br>';
echo 'Combinations: '.count($rand->combinations).'<br>';

运行此操作时,预计将有10000个唯一字符串。

我做了10次测试,结果如下:

独特字符串: 9998组合: 527879

独特字符串: 9999组合: 518899

独特字符串: 9999组合: 515290

独特字符串: 9999组合: 516193

独特字符串: 10000组合: 526652

独特字符串: 10000组合: 516049

独特字符串: 10000组合: 523217

独特字符串: 10000组合: 509729

独特字符串: 10000组合: 517236

独特字符串: 10000组合: 512270

这里的逻辑有缺陷吗?测试限制在10个字符串,但测试表明至少有10000个唯一字符串。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-12 11:40:03

代码中的几个问题:

a)记忆已使用的组合是有缺陷的

代码语言:javascript
复制
$this->combinations[$this->first.'.'.$this->second.$this->number] = 1;

对于给定的$ this ->首先,它将发生碰撞

$this->second = 10,$this->number = 11 (=1011)

$this->second = 101,$this->number =1 (=1011)

在$this->second和$this->number之间添加一个分隔符

( b)我下载的文件在你的单词列表中可能有重复,例如“绿色”这个词在形容词中是重复的。

( c)仔细调试代码的递归(自引用)字符。

( d)函数generateNumbers中参数的用途是什么($first= null,$second = null)?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33670382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档