random numbers with arrays and functions

I need to fill an array with random numbers 1-10 using a function call to generate the random numbers


#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int randNumGen(int);

int main()
{
int randNum;
int counter = 0; //counter top a count on random numbers
int myArray[11]; //array size declaration
srand(time(0));
for (int i = 0; i < 11; i++) //for loop to set parameters for array
{
int myArray[i] = randNumGen(randNum); //fills array with random number function

}
for (int i = 0; i < 11; i++)
{
cout << randNum << " ";
}

system("pause");
return 0;
}

int randNumGen(int randNum)
{
int ranNum;
static bool init = false;

if (!init)
{
srand(time(NULL));
init = true;
}
randNum = (rand() % 10) + 1;
cout << randNum << "";
return (randNum);
}
Last edited on
Check out the C++11 <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random

Click around to find examples of working code.

EDIT: You posted code while I was typing. Your code needs to be [code]between the code tags[/code], rather than between two empty pairs of code tags.
Last edited on
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
include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int randNumGen(int);

int main()
{
	int randNum; 
	int counter = 0;                    //counter to keep a count on random numbers
	int myArray[11];                   //array size declaration
	srand(time(0));
		for (int i = 0; i < 11; i++)           //for loop to set parameters for array
		{
			int myArray[i] = randNumGen(randNum);   //fills array with random number function
		}
	for (int i = 0; i < 11; i++)               //parameters for printing out the array
	{
		cout << randNum << " ";                //Array outputs the random numbers
	}

	system("pause");
	return 0;
}

int randNumGen(int randNum)
{
	int ranNum;               //holds random number
	static bool init = false;       //used to insure a random number "seed"

	if (!init)
	{
		srand(time(NULL));
		init = true;
	}
	randNum = (rand() % 10) + 1;          //generates random number between 1 and 10
	cout << randNum << "";               //prints out random number
	return (randNum);
}
@LB most people are just following instructions from their universities and they just have to use srand, but its nice that you mention the random header since its much better, and if OP is just coding to learn he should probably pick it up.

@OP, please be more specific in your next post about whats actually wrong, if its not compiling, if its wrong output etc. In this case...

int randNum; // initialize it to 0

int myArray[i] = randNumGen(randNum);

Here you're not actually filling the array, but instead you're recreating an array all the time, which doesnt work becuase "i" is not a constant value. So you want to use the array you already created. Meaning remove the "int" part of it so it looks like this

myArray[i] = randNumGen(randNum);

And lastly,
1
2
3
4
for (int i = 0; i < 11; i++)              
	{
		cout << randNum << " ";  
        }


If you fill the array with random numbers, why are you not printing out the outputs of the array, but rather a normal variable? Change that to

1
2
3
4
for (int i = 0; i < 11; i++)              
	{
		cout << myArray[i] << " ";             
	}


Now you're printing out the contents of the array.

Full Program:

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
int main()
{
	int randNum = 0;
	int counter = 0;                    //counter to keep a count on random numbers
	int myArray[11];                   //array size declaration
	srand(time(0));
	for (int i = 0; i < 11; i++)           //for loop to set parameters for array
	{
		myArray[i] = randNumGen(randNum);   //fills array with random number function
	}

	for (int i = 0; i < 11; i++)               //parameters for printing out the array
	{
		cout << myArray[i] << " ";                //Array outputs the random numbers
	}
	cout << endl;

	system("pause");
	return 0;
}

int randNumGen(int randNum)
{
	int ranNum;               //holds random number
	static bool init = false;       //used to insure a random number "seed"

	if (!init)
	{
                srand(time(NULL)); // remove this its not needed.
		init = true;
	}
	randNum = (rand() % 10) + 1;          //generates random number between 1 and 10          //prints out random number
	return (randNum);
}
thanks for the help i really appreciate it.
Topic archived. No new replies allowed.