PHP 唯一的 10 位数字 str_shuffle

发布于 2024-10-03 18:32:54 字数 290 浏览 8 评论 0原文

假设我有 100 万个 10 位唯一数字。如果我对这 100 万个数字中的每一个都进行 str_shuffle,是否仍会保持唯一性???请提出建议,


谢谢大家的澄清,但问题仍然存在。实际上,我所说的一百万个号码都是唯一的号码(事实上,它们是 10 位数字的手机号码)。我想把这百万个号码给一些客户,但我不希望它们有实际的编号。因此,我需要随机化每个数字并生成一个等效的 10 位唯一数字,我可以将其提供给客户并将映射保留在我的数据库中。我正在寻找不做的求和算法对脚本进行大量处理,否则我确信如果我遵循数组和其他内容的传统路径,脚本将会崩溃

Assuming i have a 1 million 10 digit unique numeric numbers. If i do a str_shuffle for each of the 1 million numbers , will the uniqueness still be maintained???? Please suggest


Thanks guys for some clarity,but the problem still remains.Actually, the million nos that i am talking of are all uniques numbers(infact they are mobile numbers 10 digits).i want to give these million nos to some client but i do not want them to have actual nos.Therefore,i need to randomise each number and generate an equivalent 10 digit unique number which i can give to the client and keep the mapping in my DB.I am looking for sum algo which does not do much of processing for the script otherwise i am sure the script will break down if i follow the traditional path of arrays and stuff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安静被遗忘 2024-10-10 18:32:54

不。

str_shuffle('1234567890') 可能会给您“3124567890”。

str_shuffle('9876543210') 也可能会给你“3124567890”等。

No.

str_shuffle('1234567890') might give you "3124567890".

str_shuffle('9876543210') might also give you "3124567890", etc etc.

秋意浓 2024-10-10 18:32:54

很明显,答案是否定的。

对不关心所有其他数字的单个数字的任何修改可能会导致重复。

随机洗牌就是这样一种修改,因为它只是将单个数字中的数字混合在一起,因此很有可能获得重复的数字(有想要计算概率的数学人员吗?期待对此的一些评论。)

It's pretty obvious that the answer is no.

ANY modifications to single numbers which do not care about ALL other numbers may result in duplicates.

Random shuffling is such a modification as it just mixes up the digits in a single number so chances are good to get a duplicate (Any math guys out there who want to calculate the probability? Looking forward to some comments about that.)

各自安好 2024-10-10 18:32:54

据我所知,默认情况下 str_shuffle 并不唯一。它只是随机地打乱字符串。理论上,所有 100 万个数字有可能都是相同的。

str_shuffle is not unique by default as far as I know. It just randomly shuffles the string. Theoretically it would thus be possible that all 1 million numbers will be the same.

半边脸i 2024-10-10 18:32:54

反例:

<?php

$unique_numbers = array('101', '110');

foreach($unique_numbers as $number){
    echo str_shuffle($number) . PHP_EOL;
}

我得到:

011
011

Counter example:

<?php

$unique_numbers = array('101', '110');

foreach($unique_numbers as $number){
    echo str_shuffle($number) . PHP_EOL;
}

I got:

011
011
欢烬 2024-10-10 18:32:54

问题的陈述表明您实际上可能希望打乱数组元素,而不是这些元素的内容。也许您能够保持唯一性,但通过交换数组的元素一段时间来获得随机分布的集合 - 即,将随机元素 A 与随机元素 B 交换 N 次迭代。

The statement of problem suggests you may actually wish to shuffle the array elements, and not the contents of those elements. Perhaps you'd be able to maintain uniqueness but have a randomly distributed set by swapping elements of the array for a while - ie swap random element A with random element B for N iterations.

花桑 2024-10-10 18:32:54

你要找的是一个排列脚本,我正好给你了。

function permuteString($str) 
{ 
    $aStr = str_split($str); 
    $iSize = count($aStr); 
    $aResult = array(); 

    for ($i = 0; $i < $iSize; ++$i) 
    { 
        $sFirst = array_shift($aStr); 
        $aInner = $aStr; 
        $iInner = count($aInner); 
        for ($j = 0; $j < $iInner; ++$j) 
        { 
            $aResult[] = $sFirst . implode('', $aInner); 
            $sTmp = array_shift($aInner); 
            $aInner[] = $sTmp; 
        } 
        $aStr[] = $sFirst; 
    } 
    return $aResult; 
} 

$userinput="1234567890"; 
print_r(permuteString($userinput));

这将打印给定用户输入变量的每个可能的排列。

What you're looking for is a permutation script, which I happen to have for you.

function permuteString($str) 
{ 
    $aStr = str_split($str); 
    $iSize = count($aStr); 
    $aResult = array(); 

    for ($i = 0; $i < $iSize; ++$i) 
    { 
        $sFirst = array_shift($aStr); 
        $aInner = $aStr; 
        $iInner = count($aInner); 
        for ($j = 0; $j < $iInner; ++$j) 
        { 
            $aResult[] = $sFirst . implode('', $aInner); 
            $sTmp = array_shift($aInner); 
            $aInner[] = $sTmp; 
        } 
        $aStr[] = $sFirst; 
    } 
    return $aResult; 
} 

$userinput="1234567890"; 
print_r(permuteString($userinput));

This will print every possible permutation of the given userinput variable.

孤寂小茶 2024-10-10 18:32:54

取决于你对“独特性”的定义。

当你说 100 万组 10 位数字时,如果你想对所有这些数字使用 str_shuffle,然后当你说 unique 时,你的意思是 100 万个 10 位数字中的至少 1 个电话号码在洗牌之前不存在还是什么?如果是这样,请看这里:

10!是 3628800。这比 100 万大很多。因此,甚至有三分之二的机会所有数字都是随机的。

这意味着,如果您对所有数字使用 str_shuffle,您很可能(超过 66% 的机会)获得“唯一”集,无论您将唯一定义为什么。

Depends on what you define 'uniqueness' as.

When you say the 1 million set of 10-digit numbers, if you want to use str_shuffle on all of them, and then when you say unique, you mean at least 1 of the 1 million 10-digit phone numbers didn't exist before shuffling or what? If so, look here:

10! is 3628800. That's bigger than 1 million by a lot. So there's even a 2 in 3 chance that all the numbers will be random.

This means that if you use str_shuffle on all of the numbers, you will most likely (over 66% chance) get a 'unique' set, no matter what you define unique as.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文