Random numbers in loop

I want to get a different array in every cycle of loop but this code is creating the same array every time in loop. Please help! I tried putting srand() function everywhere, in function, in main(), in loop, everywhere...
(Sorry for bad English :~)


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

using namespace std;

void randArray(int[], int); //getting random numbers in this function
void input(int[], int);
void assendSort(int[], int);
void printArray(int[], int);
bool win(int[], int[], int);

int main()
{
srand(time(NULL)); //srand() here
int lottNumbers[5], pGuess[5];
bool flag = 1;
randArray(lottNumbers, 5); //getting random numbers in this function
assendSort(lottNumbers, 5);
while (flag)
{
input(pGuess, 5);
cout << endl << "Lottry numbers: ";
printArray(lottNumbers, 5);
if (win(lottNumbers, pGuess, 5))
cout << endl << "You win!";
else
cout << endl << "You lose!";
cout << endl << endl << "Do you want to play again?(1 for yes 0 for no): ";
cin >> flag;
}
return 0;
}

void randArray(int a[], int siz) //getting random numbers in this function
{
for (int i = 0; i < siz; i++)
a[i] = rand() % 40 + 1; //getting random numbers here
}

void input(int a[], int siz)
{
cout << "Guess 5 numbers(1 to 40):" << endl;
for (int i = 0; i < siz; i++)
cin >> a[i];
}

void assendSort(int a[], int siz)
{
int minn, minIndex;
for (int i = 0; i<siz; i++)
{
minn = a[i];
minIndex = i;
for (int j = i + 1; j<siz; j++)
{
if (minn>a[j])
{
minn = a[j];
minIndex = j;
}
}
a[minIndex] = a[i];
a[i] = minn;
}
}

bool win(int lott[], int guess[], int siz)
{
for (int i = 0; i < siz; i++)
{
for (int j = 0; j < siz; j++)
{
if (guess[j] == lott[i])
return 1;
}
}
return 0;
}

void printArray(int a[], int siz)
{
for (int i = 0; i < siz; i++)
cout << a[i] << ", ";
}
You are using srand() correctly - just once at the start of main().

The problem is the loop:
1
2
3
4
5
6
7
8
9
10
11
12
    while (flag)
    {
        input(pGuess, 5);
        cout << endl << "Lottry numbers: ";
        printArray(lottNumbers, 5);
        if (win(lottNumbers, pGuess, 5))
            cout << endl << "You win!";
        else
            cout << endl << "You lose!";
        cout << endl << endl << "Do you want to play again?(1 for yes 0 for no): ";
        cin >> flag;
    }

Notice function randArray() is not called from anywhere inside the loop. Hence it will always use the same set of numbers.
oh I get it. How fool I am... :~
Topic archived. No new replies allowed.