I have made... well attempted at making a unique random number generator using arrays and functions, but it still will not work and I cannot find my error at all. Can someone please help? T_T

#include <iostream>
#include <ctime>

using namespace std;

// *****Function prototypes****

void displayArray(int randNum[], int elements);
int randNums(int randNum[], int elements);

int main ()
{

	//declare array
	int numbers[6] = {0};

	//random number generator
	srand(static_cast<int>(time(0)));
	

	randNums(numbers, 6);

	displayArray(numbers, 6);
		

	
		

system("pause");
return 0;
}
//****Functions****
int randNums(int randNum[],int elements)
{
	//declare variable
	int value = 0;

	bool checker[6]; // this number has to change if the range is to be bigger or smaller
	
	for (int sub = 0; sub < 6; sub ++) // this one as well
		{
			checker[sub] = false; //assigns false to a random number in the array set
		}

	for (int sub = 0; sub < 6; sub++) // this number changes as well
		{
			value = 1 + rand() % 6; // this number must change as well
			if(checker[value])
			{
			sub--;
			}
			else
				randNum[sub] = value;
				checker[value] = true; //if hasnt been picked it assigns that number
		}

	return value;
}

void displayArray(int randNum[], int elements)
{
	for (int sub = 0; sub < elements; sub ++)
	{
		cout << "Your lottery numbers are: " << randNum[sub] << endl;
	}
}

Recommended Answers

All 4 Replies

What is the error? Is it a compiler error? Runtime error? Logic error?

Oh sorry the problem is that it will run, but will not give the information I desire which is the 6 lottery numbers.

I ended up finishing this program if anyone would like me to post it then just ask me in this forum.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// *****Function prototypes****

void displayArray(int randNum[], int elements);
void randomNum(int randNums[], int elements);

int main ()
{

	//declare array
	int numbers[6] = {0};

	//random number generator
	srand(static_cast<int>(time(0)));
	
	randomNum(numbers, 6);

	displayArray(numbers, 6);
		
system("pause");
return 0;
}
//****Functions****
void randomNum(int randNums[], int elements)
{
    for (int i = 0; i < elements; i++)
    {
        bool same;
        do
        {
            same = false;
            randNums[i] = rand() % 6 + 1;
            // Check if the newly generated number is a duplicate:
            for (int check = 0; check < i; check++)
            {
                if (randNums[i] == randNums[check])
                {
                    same = true;
                    break;
                }
            }
        } while (same);
    }
}

void displayArray(int randNum[], int elements)
{
	for (int sub = 0; sub < elements; sub ++)
	{
		cout << "Your lottery numbers are: " << randNum[sub] << endl;
	}
}

Here is the code for this program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.