Avoiding rand to generate same numbers

Hi I tryed to create program I wich u can guess number, I made some if to prevent inputting 2 times the same number It works. I tryed to apply the same process for generating but it isn't working please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdlib>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
   const int pocet_cisel=6;
   int b=0;
   int i=0;
   int tipnute_cisla[pocet_cisel];
   int generovane_cisla[pocet_cisel];
   cout<<"Zadaj "<<pocet_cisel<<" cisel (Zrebuje sa desat cisel od 1 po 10, ti mozes tipovat 6 cisel)."<<endl;
   while(i<1)
   {
          cin>>tipnute_cisla[i];
          if((tipnute_cisla[i]>0)&&(tipnute_cisla[i]<11))
          i++;
          else
          cout<<"Neplatne cislo"<<endl;
          }
   while(i<pocet_cisel)
   {
                       cin>>tipnute_cisla[i];
                       while(b<i)
                       {
                                 if((tipnute_cisla[b]!=tipnute_cisla[i])&&(tipnute_cisla[i]>0)&&(tipnute_cisla[i]<11))
                                 b++;
                                 else
                                 {
                                     cout<<"Neplatne cislo"<<endl;
                                     cin>>tipnute_cisla[i];
                                     }
                                     }
                       i++;
                       b=0;
                                     }                              
   system("PAUSE");
   i=0;
   b=0;
//Here starts generating numbers
   while(i<1)
   {
          generovane_cisla[i]= rand() % 10+1;
          srand(time(NULL));
          if((generovane_cisla[i]>0)&&(generovane_cisla[i]<11))
          i++;
          else
         
          }
   while(i<pocet_cisel)
   {
                       generovane_cisla[i]= rand() % 10+1;
                       srand(time(NULL));
                       while(b<i)
                       {
                                 if((generovane_cisla[b]!=generovane_cisla[i])&&(generovane_cisla[i]>0)&&(generovane_cisla[i]<11))
                                 b++;
                                 else
                                 {
                                     generovane_cisla[i]= rand() % 10+1;
                                     srand(time(NULL));
                                     }
                                     }
                       i++;
                       b=0;
                                     }
//Here ends generating numbers  
   int a;
   b=0;  
   for(i=0; i<pocet_cisel; i++)
   {
   for(a=0; a<pocet_cisel; a++)
   {
   if(tipnute_cisla[i]==generovane_cisla[a])
   b++;
   }          
          }
   cout<<"Tipol si tieto cisla"<<endl;
   for(i=0; i<pocet_cisel; i++)
   {
            cout<<"1. "<<tipnute_cisla[i]<<endl;
            }
   cout<<"Vyzrebovane boli tieto cisla"<<endl;
   for(i=0; i<pocet_cisel; i++)
   {
            cout<<"1. "<<generovane_cisla[i]<<endl;
            }
   cout<<"Uhadol si "<<b<<" cisel."<<endl;
   system("PAUSE");
   return EXIT_SUCCESS;
}
This call srand(time(NULL); should only happen once in your entire program, but I see it in several places. Try making this call just once at the beginning of main and see how your program behaves.
I don't think that could help.
If you ignore advice completely, how are you to know if it works or not? His advice is correct.
Sorry my fault please could you explain me how this could happend if I repeat srand(time(NULL); ?
The time function returns a value in seconds. Unless your processor is about to melt, you program will most likely run in under a second. Thus, you are seeding the random generator to the same value over and over, meaning you will get the same random number each time.
tnx
Topic archived. No new replies allowed.