Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> generowanie liczb
right6
post 19.03.2006, 14:54:12
Post #1





Grupa: Zablokowani
Postów: 206
Pomógł: 0
Dołączył: 9.03.2006

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


mam problem jak w js, wygenerowac, w petli, iles tam liczb od 1-20, ale zeby byly rozlozone losowo, i kazda z nich wystepowala dwa razy.
wiec bedziemy miec w sumie 40 liczb.


--------------------
To ja invx sie nazywam ... przepraszam i dziekuje ...
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post 19.03.2006, 20:12:51
Post #2





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

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


Dla mnie zagmatwane.
Moje rozwiazanie:
Kod
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title></title>
        <script type="text/javascript">
            function createRandomNumbersRepeatedTwice( count, min, max )
            {
                numbers = new Array();
                var i;
                
                for ( i = 0; i < count; i++ )
                {
                    numbers[ i ] = createRandomNumber( min, max );
                }
                
                duplicatedNumbers = duplicateNumbers( numbers );
                shuffledNumbers = shuffleNumbers( duplicatedNumbers );
                
                return shuffledNumbers;
            }
            
            function shuffleNumbers( numbers )
            {
                return numbers.sort(    function randomlyCompareElements( a, b )
                                        {
                                            return Math.floor( Math.random() * 3 - 1 );
                                        } );
            }
            
            function duplicateNumbers( numbers )
            {
                return numbers.concat( numbers );
            }
            
            function createRandomNumber( min, max )
            {
                return Math.floor( Math.random() * ( max - min ) + min );
            }
        </script>
    </head>
    <body onload="alert( createRandomNumbersRepeatingTwice( 4, 1, 20 ) );">
    </body>
</html>


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 18:11