Generate a list of random numbers

Hello everyone.
I am having some issues with my homework assigment. they are asking me to generate a list of random numbers; no more than 50 (range 1-10). The user(s) need to specify how many he wants in the list. I tried using an array, but it didn't work out as i expected.

Any clue or help would be well appreciated. :D
Last edited on
Sorry, we're not mind readers. We have no clue what you tried. If you want to show us some code, we'll be happy to comment on it.
Try using


// random numbers.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
# include <iostream>
# include <ctime>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int number;

srand(50);
//sets the numbers to 50

cout<<"What is the range of numbers you would like to use?"<<endl;
cin>>number;
cin.ignore();

cout<<"The numbers are:"<<endl;

for(int i=1;i<50; i++)
{
cout<<rand()%number<<endl;
// number is used to represent the range
}
cin.get();
return 0;
}


This is what I have so far:

#include <iostream>

using namespace std;

int main()
{
const int SIZE = 50;
int Append[SIZE];
for (int &val : Append)
{
cout << " How many random numbers : ";
cin >>val;
for (int val : Append)
cout << val << endl;


system("pause");
return 0;

The output of this code gives me 50 random numbers instead of the specific amount that I specify. :l
When posting code, please use code tags. Highlight the code and click the <> button to the right of the edit window. This shows the code with line numbers and syntax highlighting.

Here is your code with tags and indented to reflect the structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    const int SIZE = 50;
    int Append[SIZE];
    for (int &val : Append)
        {
            cout << " How many random numbers : ";
            cin >>val;
            for (int val : Append)
                cout << val << endl;


            system("pause");
            return 0;


Notice that the code that prompts for the number of random numbers (lines 11-12) are inside a for loop. You don't want to prompt 50 times, so that's not right. I suggest that you just remove the loop at lines 9-10.

You are also using "val" for two different things: is it the number of random numbers, or is it an individual random number? You need two different variables to represent these two different concepts.
Topic archived. No new replies allowed.