Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Przypisywanie losowej liczby do zmiennej
Adiqus
post
Post #1





Grupa: Zarejestrowani
Postów: 28
Pomógł: 1
Dołączył: 27.01.2009

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


Jak przypisac losową liczbę (której przedział sam okreslasz) do zmiennej (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Próbowałem kodu w stylu:

  1. ...
  2. </head>
  3. <form atcion="jakis_plik.php" method="POST">
  4. <input type="text" name="min">
  5. <input type="text" name="max">
  6. <input type="submit" value="Wyslij">
  7. </form>
  8. </body>
  9. </html>


  1. <?php
  2.  
  3. $max=$_POST['max'];
  4. $min=$_POST['min'];
  5.  
  6. $liczba=rand($min, $max);
  7.  
  8. ?>


Jak probuje wyswietlic za pomoca echo zmienna $liczba to mi sie pokazuje 0 ...

Ten post edytował Adiqus 28.01.2009, 18:44:46
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Sadu2
post
Post #2





Grupa: Zarejestrowani
Postów: 229
Pomógł: 13
Dołączył: 31.05.2007

Ostrzeżenie: (10%)
X----


  1. <?php
  2.    
  3.    /**
  4.      * uniqRand
  5.      *
  6.      * Generuje tablic z losowa nie powtarzalna liczba elementow
  7.      *
  8.      * @param int $min   minimalna wartosc
  9.      * @param int $max   maksymalna wartosc
  10.      * @param int $count ile elementow
  11.      * @return array
  12.      */
  13.    function uniqRand( $min, $max, $count ) {
  14.        $aItems = array();
  15.        while( count( $aItems ) < $count ) {
  16.            $iRand = rand( $min, $max );
  17.            if( !in_array( $iRand, $aItems ) ) {
  18.                $aItems[] = $iRand;
  19.            }
  20.        }
  21.        return $aItems;
  22.    }
  23.    
  24.    $aRand = uniqRand( 1, 10, 9 );
  25.    print_r( $aRand );
  26.  
  27. ?>


http://forum.php.pl/index.php?showtopic=53665

Moim zdaniem to się powinno przydać.
Go to the top of the page
+Quote Post

Posty w temacie


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: 6.10.2025 - 09:58