vendredi 30 octobre 2015

Php shuffle 2 unique numbers

I got a database with users and numbers.

I built a shuffle and tried to randomly give a user a number, that worked.

Now I have the issue that I want to give the user 2 random numbers with shuffle which shall be unique.

Main Code looks like this:

    $numbers = range(0, $counts); // $counts are the number of users in my databse
    $numbers2 = range(0, $counts);
    shuffle($numbers);
    shuffle($numbers2);

    foreach($users as $user) {
        # Starts from 1 if higher than 52
        $uniqueRand  = (array_pop($numbers)+$currentWeek) % 52 + 1;
        $uniqueRand2 = (array_pop($numbers2)+$currentWeek) % 52 + 1;

        # tried something like this but did         
        while($uniqueRand == $uniqueRand2) {
            $numbers2 = range(0, $counts);
            shuffle($numbers2);
            $uniqueRand2 = (array_pop($numbers2)+$currentWeek) % 52+1;
        }

         ...# Storing in database
    }

Here a little sketch of the application

Users      |       numbers
__________________________
Frank      |  14, 24
Tim        |  21, 43
Tom        |  52, 6
Hanz       |  8,  3
Benjamin   |  5,  1
West       |  7,  6
Thompson   |  4,  9
.....

The first line of numbers 14,21,52... stand for $numbers and the second line for $numbers2, I want to create unique random values which do not repeat themselves, but how do I check if the $number do not repeat in the same line and are still vertical unique.

So for instance would something like this be wrong:

Users      |       numbers
_________________________
Frank      |       14, 14

Something like this would be wrong too:

Users      |       numbers
_________________________
Frank      |     14, 24
Tim        |     21, 14
Tom        |     14,  6
Hanz       |      8,  3
Benjamin   |      5,  14
West       |      14,  6
Thompson   |      4,  9




Aucun commentaire:

Enregistrer un commentaire