继续生成随机数,直到不存在 - PHP

时间:2022-11-23 09:12:43

I have a string with a lot of different numbers. I am trying to create a new random number and add it to the string.

我有一个包含许多不同数字的字符串。我正在尝试创建一个新的随机数并将其添加到字符串中。

The part I need help with is "if the number already exists in the string, create a new random number, and keep doing it until a number is created that does not yet exist in the string".

我需要帮助的部分是“如果数字已经存在于字符串中,则创建一个新的随机数,并继续这样做直到创建一个在字符串中尚不存在的数字”。

// $string contains all the numbers separated by a comma
$random = rand(5, 15);

$existing = strpos($string, $random);

if ($existing !== false) { $random = rand(5, 15); }

$new_string = $string.",".$random;

I know this isn't quite right as it will only check if it's existing once. I need it to keep checking to make sure the random number does not exist in the string. Do I use a while loop? How would I change this to work properly?

我知道这不太正确,因为它只会检查它是否存在一次。我需要它继续检查以确保字符串中不存在随机数。我是否使用while循环?如何更改此功能?

Your help is much appreciated.

非常感谢您的帮助。

2 个解决方案

#1


2  

A solution that works like Endijs ... but I want to post that :)

像Endijs一样的解决方案......但我想发布:)

$string = '6,7,8';
$arr = explode(',', $string);

$loop = true;
while($loop) {
    $randomize = rand(5, 15);
    #var_dump($randomize);
    $loop = in_array($randomize, $arr);
    if (!$loop) {
        $arr[] = $randomize;
    }
}

$newString = implode(',', $arr);
var_dump($newString);

#2


0  

Checking data in string is not the best solution. Thats because if your random number will be '5', and in string you will have 15, strpos will find accurance of 5. I would convert string to array and do search on it.

检查字符串中的数据不是最佳解决方案。那是因为如果你的随机数将是'5',并且在字符串中你将有15,strpos将找到5的准确性。我会将字符串转换为数组并对其进行搜索。

$a = explode(',' $your_string);
$random = rand(5, 15);  
while (in_array($random, $a))
{
    $random = rand(5, 15);      
}
$a[] = $random;
$your_string = implode(',', $a);

Update - just be careful - if all possible variables will be already in string, it will be endless loop.

更新 - 请注意 - 如果所有可能的变量都已经在字符串中,那么它将是无限循环。

#1


2  

A solution that works like Endijs ... but I want to post that :)

像Endijs一样的解决方案......但我想发布:)

$string = '6,7,8';
$arr = explode(',', $string);

$loop = true;
while($loop) {
    $randomize = rand(5, 15);
    #var_dump($randomize);
    $loop = in_array($randomize, $arr);
    if (!$loop) {
        $arr[] = $randomize;
    }
}

$newString = implode(',', $arr);
var_dump($newString);

#2


0  

Checking data in string is not the best solution. Thats because if your random number will be '5', and in string you will have 15, strpos will find accurance of 5. I would convert string to array and do search on it.

检查字符串中的数据不是最佳解决方案。那是因为如果你的随机数将是'5',并且在字符串中你将有15,strpos将找到5的准确性。我会将字符串转换为数组并对其进行搜索。

$a = explode(',' $your_string);
$random = rand(5, 15);  
while (in_array($random, $a))
{
    $random = rand(5, 15);      
}
$a[] = $random;
$your_string = implode(',', $a);

Update - just be careful - if all possible variables will be already in string, it will be endless loop.

更新 - 请注意 - 如果所有可能的变量都已经在字符串中,那么它将是无限循环。