Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Uproszczenie pętli
Dariusz_512
post
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.04.2007

Ostrzeżenie: (0%)
-----


Mam kod

  1. <?
  2.  
  3. while(($num == $num1) or ($num == $num2) or ($num == $num3) or ($num == $num4) or ($num == $num5) or ($num1 == $num2) or ($num1 == $num3) or ($num1 == $num4) or ($num1 == $num5) or ($num2 == $num3) or ($num2 == $num4) or ($num2 == $num5) or ($num3 == $num4) or ($num3 == $num5) or ($num4 == $num5))
  4.    {
  5.    $num = rand(1,6);
  6.    $num1 = rand(1,6);
  7.    $num2 = rand(1,6);
  8.    $num3 = rand(1,6);
  9.    $num4 = rand(1,6);
  10.    $num5 = rand(1,6);
  11. }
  12.  
  13. echo $num.'</br>';
  14. echo $num1.'</br>';
  15. echo $num2.'</br>';
  16. echo $num3.'</br>';
  17. echo $num4.'</br>';
  18. echo $num5.'</br>';
  19.  
  20. ?>


Nie da się prościej ? (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Chodzi oczywiście tylko o ustawienie 6 liczb, od "1" do "6" w losowej kolejności.

Ten post edytował Dariusz_512 10.01.2009, 22:42:53
Go to the top of the page
+Quote Post
kornel-php
post
Post #2





Grupa: Zarejestrowani
Postów: 210
Pomógł: 20
Dołączył: 7.07.2006
Skąd: Krotoszyn

Ostrzeżenie: (0%)
-----


Możesz wrzucić wyniki losowania do tablicy i sprawdzać przez in_array()" title="Zobacz w manualu PHP" target="_manual, czy już istnieją.
Go to the top of the page
+Quote Post
AdvMDev
post
Post #3





Grupa: Zarejestrowani
Postów: 15
Pomógł: 1
Dołączył: 21.09.2008
Skąd: Kielce

Ostrzeżenie: (0%)
-----


  1. <?php
  2. $array = array(1,2,3,4,5,6);
  3. shuffle($array);
  4. ?>


Ten post edytował AdvMDev 10.01.2009, 22:46:27
Go to the top of the page
+Quote Post
Dariusz_512
post
Post #4





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.04.2007

Ostrzeżenie: (0%)
-----


Działa! Dzięki...
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 30.09.2025 - 20:56