Array of random numbers

I'm trying to make an array that uses a basic for look to fill up the array with a random number.

currently i have:

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

using namespace std;

int main()
{

int RandNum[30000] = rand() % 100 + 1;

for (int a = 0; a < 30000; a++)
{
cout << RandNum [a] << endl;
}

system("PAUSE");
return (0);

}

(sorry, adding a code snipit does not want to work for me atm)

When i compile and run, i get an "invalid initializer" error.

I'm assuming its because i created randnum as an array?

Any ideas? thanks.

Also is there a way to have it generate decimal numbers aswell?

Or would it just keep generating like .23, .22, .41, .42 etc due to how many decimal number there are?
Your topic is marked as solved, so you still need help?

I'm assuming the answer is 'yes'.

Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

You should call srand() once at the start of your program, and then never again.

You can't make the array contain random numbers in one line like that - you need to loop through it and manually assign the random number to each index.

By the way, rand() and srand() are being deprecated - don't use them anymore. Instead, use the new random generators available in the <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random
1
2
3
4
5
6
7
8
9
10
11
//initialize random seed:
srand (time(NULL));

int RandNum[30000];
for (int a = 0; a < 30000; a++)
{
    RandNum[a] = rand() % 100 + 1;
    cout << RandNum[a] << endl;
}
cout << cin.ignore();
return (0);


Like LB said, so try that one instead. Using cin.ignore() instead of system pause because i've heard bad things about system pause previously.
Last edited on
Topic archived. No new replies allowed.