Un code vaut mieux que du blabla...
Je souhaite insérer aléatoirement des '0' et des '1' dans un tableau à 2 dimensions (plateau)...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <script language="javascript"> var plateau=new Array(); while(y!=10) { for (var ligne=0;ligne<=8;ligne++) { plateau[ligne]=new Array(Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random())); } var y=0; for (var t=0;t<=8;t++) { for (var x=0;x<=8;x++) { y += plateau[t][x]; } } document.write(y+'<br/>'); } </script>
Mon script fonctionne mais comme je veux limiter le nombre de '1' dans le tableau à exactement 10 unités, j'ai décidé de répéter l'opération aléatoire jusqu'à ce que cela arrive...
or mon code prends beaucoup de ressources à l'exécution pour ne jamais arriver au résultat escompté...
Avez-vous une solution dans les grandes lignes pour moi?
Ensuite il y a cette ligne que je souhaite simplifier au maximum sans savoir trop comment faire:
Un grand merci à ceux et celles qui prendront le temps de regarder mon ptit problème...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 plateau[ligne]=new Array(Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()),Math.round(Math.random()));
A+![]()
Partager