Random genarate

 To generate two random values from two arrays, check if these values are not in a main array, and if they are in the main array, generate new random values until they are unique, you can use the following PHP code:


### PHP Code


```php

<?php

function generateUniqueRandomPairs($array1, $array2, $mainArray) {

    $attempts = 0;

    $maxAttempts = 1000; // To avoid infinite loop, set a maximum number of attempts


    do {

        // Increment attempts counter

        $attempts++;


        // Generate random values from each array

        $randomValue1 = $array1[array_rand($array1)];

        $randomValue2 = $array2[array_rand($array2)];


        // Create a pair

        $randomPair = [$randomValue1, $randomValue2];


        // Check if the pair is not in the main array

        $isUnique = true;

        foreach ($mainArray as $mainPair) {

            if ($mainPair === $randomPair) {

                $isUnique = false;

                break;

            }

        }


        // Break if unique pair is found or max attempts reached

        if ($isUnique || $attempts >= $maxAttempts) {

            break;

        }


    } while (true);


    if ($attempts >= $maxAttempts) {

        throw new Exception("Failed to generate unique random pair after $maxAttempts attempts.");

    }


    return $randomPair;

}

//------single unique value check-----//

function generateUniqueRandomValues($array1, $array2, $mainArray) {

    $attempts = 0;

    $maxAttempts = 1000; // To avoid infinite loop, set a maximum number of attempts


    do {

        // Increment attempts counter

        $attempts++;


        // Generate random values from each array

        $randomValue1 = $array1[array_rand($array1)];

        $randomValue2 = $array2[array_rand($array2)];


        // Check if the random values are not in the main array

        $isUnique1 = !in_array($randomValue1, $mainArray);

        $isUnique2 = !in_array($randomValue2, $mainArray);


        // Break if unique values are found or max attempts reached

        if ($isUnique1 && $isUnique2 || $attempts >= $maxAttempts) {

            break;

        }


    } while (true);


    if ($attempts >= $maxAttempts) {

        throw new Exception("Failed to generate unique random values after $maxAttempts attempts.");

    }


    return [$randomValue1, $randomValue2];

}



//------------//

// Example usage

$array1 = [1, 2, 3, 4, 5];

$array2 = ['a', 'b', 'c', 'd', 'e'];

//$mainArray[] = [$row['id'], $row['value']];

$mainArray = [

    [1, 'a'],

    [2, 'b'],

    [3, 'c']

];


try {

    $uniqueRandomPair = generateUniqueRandomPairs($array1, $array2, $mainArray);

    print_r($uniqueRandomPair);

} catch (Exception $e) {

    echo $e->getMessage();

}


?>

```


### Explanation


1. **Function Definition**: The `generateUniqueRandomPairs` function takes three arguments: two arrays (`array1` and `array2`) from which to generate random values, and a `mainArray` containing pairs that should be avoided.


2. **Random Pair Generation**: Inside a `do-while` loop, the function generates a random value from each of the two arrays and forms a pair.


3. **Uniqueness Check**: The function checks if this pair is already present in the `mainArray`. If it is not present, it considers the pair unique. If the pair is present, it continues generating new pairs until a unique one is found or the maximum number of attempts (`$maxAttempts`) is reached.


4. **Handling Failures**: If the function fails to generate a unique pair after the maximum number of attempts, it throws an exception to avoid an infinite loop.


5. **Example Usage**: The example shows how to use the function with sample arrays and a main array containing pairs to be avoided.


This approach ensures that you get a unique random pair from the two arrays without using recursion. Adjust the arrays and the maximum number of attempts as needed for your specific use case.

Previous
Next Post »