Jump to content

Php!? How to generate a random numbers (4) into array!?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Stasonix

Stasonix

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal
for example it must be look like:


 $randNum[1] = 2;

 $randNum[2] = 4;

 $randNum[3] = 1;

 $randNum[4] = 3;


next time it will another numbers, but it must be only 4,3,2 or 1.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
and it shall only occur once, so what you want is to make a randomized reorder of the values?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
PHP: mt_rand - Manual
PHP: shuffle - Manual
<?PHP
    $randNum = array(mt_rand(1,4), mt_rand(1,4), mt_rand(1,4), mt_rand(1,4));
    print_r($randNum);
    
    $randNum = array(1,2,3,4);
    shuffle($randNum);
    print_r($randNum);





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users