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
LBO
post 19.03.2006, 16:37:38
Post #2





Grupa: Zarejestrowani
Postów: 1 415
Pomógł: 117
Dołączył: 7.09.2005
Skąd: Warszawa

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


daleko temu od optymalnego rozwiazania (I think, ale funkcja wykonuje to o co prosiles):

Kod
<script>
//generates random integer <start, end)
function rand(intStart, intEnd) {
   return intStart + Math.floor(Math.random() * (intEnd - intStart));
}

function right6Generator(intStart, intEnd) {
   var arrNumbers = new Array();
   var i = 0;
   while (intStart <= intEnd) {
       arrNumbers[i++] = intStart;
       arrNumbers[i++] = intStart++;
   }
   /*document.write('------------arrNumber--------------<br />');
   for (i = 0; i < arrNumbers.length; i++) {
       document.write(arrNumbers[i] + '<br />');
   }*/
   var arrResult = new Array();
   for (i = arrNumbers.length - 1; i >= 0; i--) {
       intIndex = rand(0, i + 1);
       arrResult[i] = arrNumbers.splice(intIndex, 1);
   }
   /*document.write('------------arrResult--------------<br />');
   for (i = 0; i < arrResult.length; i++) {
       document.write(arrResult[i] + '<br />');
   }*/
   return arrResult;
}
</script>


wykorzystanie (dla twojego przykladu):

Kod
right6Generator(1, 20);


edit: wykomentowane czesci kodu mozesz usunac, sluza tylko do debugowania...

Ten post edytował LBO 19.03.2006, 16:48:07
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 - 15:59