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 (1 - 8)
Lion_87
post
Post #2





Grupa: Zarejestrowani
Postów: 850
Pomógł: 120
Dołączył: 15.02.2007
Skąd: Łódź

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


hee a jesteś pewnien że post jest w ()(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) a nie przypadkiem [](IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

i to tez zle
Cytat
mothod


Ten post edytował Lion_87 28.01.2009, 18:36:45
Go to the top of the page
+Quote Post
Adiqus
post
Post #3





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

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


pisalem ten teat na szybko ... w w prawdziwym kodzie nie popelnilem takich bledow ...
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #4





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


1. Nie wpisuje sie kodu tutaj jeszcze raz tylko KOPIUJE - oszczedzisz NAM czas na szukaniu bledow ktore wprowadziles PRZEPISUJAC kod!
2. Pokaz twoj prawdziwy kod
Go to the top of the page
+Quote Post
Adiqus
post
Post #5





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

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


poprawilem ... w pierwszym poscie ...
Go to the top of the page
+Quote Post
Lion_87
post
Post #6





Grupa: Zarejestrowani
Postów: 850
Pomógł: 120
Dołączył: 15.02.2007
Skąd: Łódź

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


a
Cytat
atcion
(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)



Cytat
w prawdziwym kodzie nie popelnilem takich bledow ...

taaa

Ten post edytował Lion_87 28.01.2009, 18:49:17
Go to the top of the page
+Quote Post
Sadu2
post
Post #7





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
Adiqus
post
Post #8





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

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


$count - to liczba liczb ktore maja zostac wygenerowane (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) tak (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) czy jak (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
erix
post
Post #9





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Tak. Choć to trochę mało wydajny kod, gdyż wymaga liczenia elementów tablicy za każdym razem...
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: 22.08.2025 - 17:43