Erreur de probabilité

Résolu
cs_ProgVal Messages postés 33 Date d'inscription dimanche 23 avril 2006 Statut Membre Dernière intervention 22 octobre 2006 - 24 avril 2006 à 16:43
cs_ProgVal Messages postés 33 Date d'inscription dimanche 23 avril 2006 Statut Membre Dernière intervention 22 octobre 2006 - 24 avril 2006 à 17:53
Bonjour,
J'utilise Random définir aléatoirement les 4 valeurs de mon tableau. Problème: 9 fois sur 10, le tableau est: {6,8,0,6}. Ce nombre n'est donc pas vraiment aléatoire. Pouvez-vous me dire comment faire? Merci d'avance.

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int compte_tours;
int choix[4]; //tableau random


Randomize();
for (compte_tours = 0;compte_tours <= RadioGroup1->ItemIndex; compte_tours++)
{
choix[compte_tours] = random(10);
}
}
//---------------------------------------------------------------------------

Ps: j'utilise C++ Builder 3

4 réponses

meech Messages postés 209 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 14 août 2007
24 avril 2006 à 16:54
Salut,

Tu omets l'initialisation aléatoire du randomize() : typiquement, on peut utiliser la fonction srand avec le timestamp courant en paramètre (il change en permanence). Exemple :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define randomize() (srand(time(0)))
#define random(x) (rand() % x)


int main(int argc, char *argv[])
{
randomize();
printf("%d\n", random(10));
system("PAUSE");
return 0;
}
3
cs_ProgVal Messages postés 33 Date d'inscription dimanche 23 avril 2006 Statut Membre Dernière intervention 22 octobre 2006
24 avril 2006 à 17:23
A présent j'ai essayé de récupérer les centièmes de l'horloge de mon système, comme ceci:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int compte_tours;
struct time t;



for (compte_tours = 0;compte_tours <= RadioGroup1->ItemIndex; compte_tours++)
{
gettime(&t);
choix[compte_tours] = t.ti_hund%10;
}
}
//---------------------------------------------------------------------------

Mais les 4 valeurs de mon tableau sont identiques (logique car elles ont été prisent en même temps)
3
cs_ProgVal Messages postés 33 Date d'inscription dimanche 23 avril 2006 Statut Membre Dernière intervention 22 octobre 2006
24 avril 2006 à 17:52
J'ai trouvé la réponse! il suffit d'appuyer 4 fois sur mon Button1 voici donc mon unité:

//---------------------------------------------------------------------------
#include <stdlib.h>
#include <vcl.h>
#pragma hdrstop


#include <stdio.h>
#include <dos.h>



#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int choix[4]; //tableau random
int test[4]; //tableau à tester, choisi par l'utilisateur dans les ComboBox
int nb_juste, nb_mal_places;
int compte_tours_clique;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
compte_tours_clique=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{


struct time t;


if (compte_tours_clique<=4)
{
gettime(&t);
choix[compte_tours_clique] = t.ti_hund%10;
compte_tours_clique = compte_tours_clique+1;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
test[1] = 0;
test[2] = 0;
test[3] = 0;
test[4] = 0;
int test[4]; //tableau à tester, choisi par l'utilisateur dans les ComboBox
int compte_tours, compte_tours2;


if (RadioGroup1->ItemIndex >= 0)
{
test[0] = StrToInt(ComboBox1->Text);
}
if (RadioGroup1->ItemIndex >= 1)
{
test[1] = StrToInt(ComboBox2->Text);
}
if (RadioGroup1->ItemIndex >= 2)
{
test[2] = StrToInt(ComboBox3->Text);
}
if (RadioGroup1->ItemIndex >= 3)
{
test[3] = StrToInt(ComboBox4->Text);
}


for (compte_tours = 0;compte_tours <= RadioGroup1->ItemIndex; compte_tours++)
{
if (choix[compte_tours] == test[compte_tours])
{
nb_juste = nb_juste+1;
}
for (compte_tours2 = 0;compte_tours2 <= RadioGroup1->ItemIndex; compte_tours2++)
{
if ((test[compte_tours] == choix[compte_tours2]) && (compte_tours != compte_tours))
{
nb_mal_places = nb_mal_places + 1;
}
}
}
Label5->Caption = nb_juste;
Label6->Caption = nb_mal_places;
Label8->Caption = (RadioGroup1->ItemIndex + 1) - (nb_juste + nb_mal_places);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
ShowMessage ("Appuyer 4 fois sur lancer");
}
//---------------------------------------------------------------------------

Merci quand même à Meech
3
cs_ProgVal Messages postés 33 Date d'inscription dimanche 23 avril 2006 Statut Membre Dernière intervention 22 octobre 2006
24 avril 2006 à 17:53
Etant donné que le timer sont impressi, je pouvait aussi en utiliser comme mes plusieur clicks
3
Rejoignez-nous