MAJ les valeurs retournées par une fonction

MAJ les valeurs retournées par une fonction - C - Programmation

Marsh Posté le 17-05-2007 à 17:31:48    

Salut salut,
 
j'ai un p'tit problème. J'ai créé une fonction qui me retourne une suite de numéro aléatoire dans un tableau. Du style :

Code :
  1. int alea()
  2. {
  3.   ...
  4.   return(numero)
  5. }


J'appel cette fonction par ma fonction principal du style :

Code :
  1. int main()
  2. {
  3.   palea=alea();
  4.   printf(données de palea);
  5.   ....
  6. }


 
et je fais les opérations que je veux dessus, tout se passe bien...
Maintenant quand je veux faire tourner plusieurs fois ma fonction, dans ce style :

Code :
  1. int main()
  2. {
  3.   while(...)
  4.    {
  5.       palea=alea();
  6.       printf(données de palea);
  7.       ....
  8.     }
  9. }


 
les données affichées sont les mêmes pendant plusieurs cycles, il fo que j'en mette presque 2000 pour que ca change.
J'ai bien essayé de mettre une pause pour donner le temps a la fonction de se MAJ entre deux appels mais je ne peux pas me permettre de poser un getchar()... et de devoir payer un mec pour presser sur les touches ... :sarcastic:  
 
Une solution ???!
 
Merci d'votre Help !!

Reply

Marsh Posté le 17-05-2007 à 17:31:48   

Reply

Marsh Posté le 17-05-2007 à 17:33:16    

sans le contenu d'alea on pourra pas t'aider...


---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 17-05-2007 à 18:18:06    

dsl, voila la fonction alea   :jap:  
 
Petite explication : cette fonction genere une suite de 7 nombres tirés au hasard mais differents les un des autres, en clair c'est une sorte de Loto
 

Code :
  1. #define nb 7
  2. int alea()
  3. {
  4.   int i,j,r;
  5.   int eng[50];
  6.   int numero[nb];
  7.   j=0;
  8.   srand(time(0));
  9. /*je m'assure de bien tout mettre à zero*/
  10.   for(i=0;i<7;i++) numero[i]=0;
  11.   for(i=1;i<50;i++) eng[i]=0;
  12.   while(j<nb)                      /*jusqu'a ce qu'on en ait 7*/
  13.    {
  14.       r=50*(float)rand()/RAND_MAX;          /*tindindin tindindin... c'est lequel qui va sortir ???*/
  15.       if(eng[r]==0)                /*verifie que le numero n'a jamais ete tire*/
  16.       {
  17.          eng[r]=1;                  /*on memorise que ce numero a ete tire*/
  18.          numero[j]=r;              /*on enregistre le numero*/
  19.          j++;                         /*.. et un numero de moins a tirer*/
  20.       }
  21.     }
  22. return(numero);
  23. }


 
Euh j'espere que ca vous eclaire  :p  
 
Tchüss

Reply

Marsh Posté le 17-05-2007 à 18:20:16    

srand(time(0));
 
tu crois que ça change tout les combien ça ?


---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 17-05-2007 à 18:29:14    

beh che pô !
pas tres souvent j'imagine .... je pensais que c'etait juste l'amorcage du srand...  
tu m'expliques vite fait ?

Reply

Marsh Posté le 17-05-2007 à 18:32:46    

ce qui est etonnant c'est que j'ai une suite de 7 nombres qui sont bien distinct mais c'est toujours la meme suite, che cha mon probleme ...
me trompe je ?

Reply

Marsh Posté le 17-05-2007 à 18:35:06    

bensmash a écrit :

beh che pô !
pas tres souvent j'imagine .... je pensais que c'etait juste l'amorcage du srand...  
tu m'expliques vite fait ?


ça change aussi souvent que la résolution de time le permet.
 
Tu peux améliorer (mais plus ANSI) avec un srand(time(NULL) ^ getpid());

Reply

Marsh Posté le 17-05-2007 à 18:35:26    


NAME
       rand, rand_r, srand - pseudo-random number generator

 

SYNOPSIS
       #include <stdlib.h>

 

      int rand(void);

 

      int rand_r(unsigned int *seedp);

 

      void srand(unsigned int seed);

 

DESCRIPTION
       The  rand()  function  returns  a  pseudo-random  integer between 0 and
       RAND_MAX.

 

      The srand() function sets its argument as the seed for a  new  sequence
       of  pseudo-random  integers  to be returned by rand().  These sequences
       are repeatable by calling srand() with the same seed value.

 

      If no seed value is provided,  the  rand()  function  is  automatically
       seeded with a value of 1.

 

      The function rand() is not reentrant or thread-safe, since it uses hid-
       den state that is modified on each call. This might just  be  the  seed
       value to be used by the next call, or it might be something more elabo-
       rate. In order to get reproducible behaviour in a threaded application,
       this  state  must  be  made explicit. The function rand_r() is supplied
       with a pointer to an unsigned int, to be used as state.  This is a very
       small  amount  of  state, so this function will be a weak pseudo-random
       generator. Try drand48_r(3) instead.

 

RETURN VALUE
       The rand()  and  rand_r()  functions  return  a  value  between  0  and
       RAND_MAX.  The srand() function returns no value.

 

EXAMPLE
       POSIX.1-2001 gives the following example of an implementation of rand()
       and srand(), possibly useful when one needs the same  sequence  on  two
       different machines.

 

          static unsigned long next = 1;

 

          /* RAND_MAX assumed to be 32767 */
           int myrand(void) {
               next = next * 1103515245 + 12345;
               return((unsigned)(next/65536) % 32768);
           }

 

          void mysrand(unsigned seed) {
               next = seed;
           }

 

NOTES
       The  versions of rand() and srand() in the Linux C Library use the same
       random number generator as random() and srandom(), so  the  lower-order
       bits  should  be as random as the higher-order bits.  However, on older
       rand() implementations, and on  current  implementations  on  different
       systems,  the  lower-order  bits  are much less random than the higher-
       order bits.  Do not use this function in applications  intended  to  be
       portable when good randomness is needed.

 

      In  Numerical Recipes in C: The Art of Scientific Computing (William H.
       Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New
       York:  Cambridge University Press, 1992 (2nd ed., p. 277)), the follow-
       ing comments are made:
              "If you want to generate a random integer between 1 and 10,  you
              should always do it by using high-order bits, as in

 

                    j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));

 

             and never by anything resembling

 

                    j = 1 + (rand() % 10);

 

             (which uses lower-order bits)."

 

      Random-number  generation is a complex topic.  The Numerical Recipes in
       C book (see reference above) provides an excellent discussion of  prac-
       tical random-number generation issues in Chapter 7 (Random Numbers).

 

      For  a  more  theoretical  discussion  which also covers many practical
       issues in depth, see Chapter 3 (Random Numbers) in  Donald  E.  Knuth's
       The  Art  of Computer Programming, volume 2 (Seminumerical Algorithms),
       2nd ed.; Reading,  Massachusetts:  Addison-Wesley  Publishing  Company,
       1981.

 

CONFORMING TO
       The  functions  rand()  and  srand() conform to SVr4, 4.3BSD, C89, C99,
       POSIX.1-2001.  The function rand_r() is from POSIX.1-2001.

 

SEE ALSO
       drand48(3), random(3)


Message édité par 0x90 le 17-05-2007 à 18:36:14

---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 17-05-2007 à 22:07:34    

srand() il faut l'appeler une seule fois dans ton programme.

Reply

Marsh Posté le 17-05-2007 à 23:38:42    

matafan a écrit :

srand() il faut l'appeler une seule fois dans ton programme.


Je pense que c'etait la réponse qu'il fallait...Merci !!
En étudiant la doc qu'on m'a donné je suis arrivé à cette conclusion tout a l'heure...  
Mais MERCI paske certains arrivent à être moins clairs avec plus de phrases et te prennent accessoirement pour un c** ... m'enfin...

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed