A voir également:
- Random chiffres differents
- Comment activer les chiffres du clavier - Guide
- Tous les code possible de 0 à 9 (4 chiffres ) liste - Forum Jeux vidéo
- Combien de combinaison possible avec 3 chiffres - Forum Programmation
- Identifiant 10 chiffres banque postale ✓ - Forum Réseaux sociaux
- France travail code personnel 6 chiffres - Forum Réseaux sociaux
3 réponses
Si c'est en pascal, je t'ai écrit le programme dont l'astuce est la suivante: Tu dis au programme de choisir un 1er chiffre. Le deuxième sera choisi en fonction du premier: c a d que le programme ne prend ce chiffre que lorsqu'il est différent du premier. J'ai utilisé la fonction repeat pour dire au pascal de répéter le choix aléatoire du deuxième chiffre jusqu'à ce que celui-ci soit différent du premier et ainsi de suite pour les deux autres.
program grgq;
uses wincrt;
Var a,b,c,d: integer;
Begin
randomize;
a:=random(10);
b:=random(10);
if (b=a) then begin randomize;
repeat b:=random(10) until (b<>a);
end;
c:=random(10);
if (c=a) or (c=b) then begin randomize;
repeat c:=random(10) until (c<>a) and (c<>b);
end;
d:=random(10);
if (d=a) or (d=b) or (d=c) then begin randomize;
repeat d:=random(10) until (d<>a) and (d<>b) and (d<>c);
end;
writeln(a,b,c,d);
end.
program grgq;
uses wincrt;
Var a,b,c,d: integer;
Begin
randomize;
a:=random(10);
b:=random(10);
if (b=a) then begin randomize;
repeat b:=random(10) until (b<>a);
end;
c:=random(10);
if (c=a) or (c=b) then begin randomize;
repeat c:=random(10) until (c<>a) and (c<>b);
end;
d:=random(10);
if (d=a) or (d=b) or (d=c) then begin randomize;
repeat d:=random(10) until (d<>a) and (d<>b) and (d<>c);
end;
writeln(a,b,c,d);
end.
et en C ça donne ça:
#include <stdio.h>
int main()
{
int ch1,ch2,ch3,ch4;
srand (time (NULL));
ch1=(rand()%8)+1;
do{
ch2=(rand()%8)+1;
}while(ch2==ch1);
do{
ch3=(rand()%8)+1;
}while(ch3==ch1 || ch3==ch2);
do{
ch4=(rand()%8)+1;
}while(ch4==ch1 || ch4==ch2 || ch4==ch3);
printf("chiffre 1: %d\nchiffre 2: %d\nchiffre 3: %d\nchiffre 4: %d\n",ch1,ch2,ch3,ch4);
return 0;
}