vendredi 29 juillet 2016

Using unique random numbers for a switch statement

I need a function or an array that gives me random numbers from 1 - 47, so I can use it for my code below.

public function output($question_id, $solution) {
    $yes = ($GLOBALS['TSFE']->sys_language_uid == 0) ? 'Ja' : 'Oui';
    $no = ($GLOBALS['TSFE']->sys_language_uid == 0) ? 'Nein' : 'No';
    switch ($question_id) {
        case 1:
            $arg = array(
                'main_content' => $this->build_html(1, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
            );
        break;
        case 2:
            $arg = array(
                'main_content' => $this->build_html(2, '02_0342_05_14_Psori_Arthropathie.jpg', $yes, $no, $solution),
            );
        break;
        case 3:
            $arg = array(
                'main_content' => $this->build_html(3, '03_0255_05_14_Psori_Arthropathie.jpg', $yes, $no, $solution),
            );
        break;
    }
}

Example

This

case 1:
    $arg = array(
        'main_content' => $this->build_html(1, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
    );

should look somehow like this:

case $random_id:
    $arg = array(
        'main_content' => $this->build_html($random_id, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
    );

So that means, each case and each first parameter of the function build_html should get a unique random id.

Of course I could use rand() but then it is quite possible that I get duplicated values.

Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire