0

I'm building a program in which a user types in a number (n) and a set of random numbers is created. So for example, if a user inputs 8, then eight random numbers should be created and they should range from 0-999,999. The program seems to be compiling, the only problem is, only one random number is being generated.

#include <iostream>
#include <vector>
#include <cstdlib> 

using namespace std;

main()
{
    int n;
    int r;
    int i;
    int j;
    vector<int> v;

    cout << "Enter size of vector: ";
    cin >> n;

    for (i = 0; i < n; i++)
    {
        v.push_back(n);
        r = rand() % 1000000;
        v[i] = r;
    }

    cout << r << endl;

Can anyone tell me what I'm doing wrong and what I need to do for more than one random number to be generated?

UndefinedReference
  • 1,223
  • 4
  • 22
  • 52

4 Answers4

4

What's wrong with the obvious:

for (int i=0; i<n; i++)
    v.push_back(rand()%1000000);

It looks like you're generating the right quantity of random numbers, but when you're done, you're printing r instead of v, which is what contains the random numbers.

Edit: std::vector doesn't support operator<< directly, so you can use a loop to print out the contents:

for (int i=0; i<v.size(); i++)
    std::cout << v[i] << '\n';

or you can use std::copy:

std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));

There are, of course, a variety of other possibilities as well...

Edit 2: Here's a complete/correct version of what Chris Lutz suggested in his comment:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include "infix_iterator.h"

template <typename T>
std::ostream& operator<<(std::ostream &o, const std::vector<T>& v) { 
    o << "[";
    std::copy(v.begin(), v.end(), infix_ostream_iterator<T>(o, ", ")); 
    o << "]";
    return o; 
}

#ifdef TEST
int main() { 

    std::vector<int> x;

    for (int i=0; i<20; i+=2)
        x.push_back(i);

    std::cout << x << "\n";
    return 0;
}
#endif

Though it's not strictly necessary, this uses an ostream_infix_iterator I posted some time ago.

Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • @Jerry Coffin - I tried putting v in place of r in the cout statement but when I run the program, the only output is "0". – UndefinedReference Jan 29 '11 at 01:20
  • @Meursault- You shouldn't be able to print out a vector with `cout << v << endl;` and I'm surprised that's compiling at all. To print the vector, just use a for loop over the elements: `for (size_t i = 0; i < v.size(); ++v) cout << v[i] << endl;` – templatetypedef Jan 29 '11 at 01:22
  • @templatetypedef - You _can't_ print a vector with `cout << v << endl;` but you most certainly _should_ be able to do so. – Chris Lutz Jan 29 '11 at 01:28
  • @Chris Lutz- True... that was a poor choice of words. By "shouldn't be able to" I meant "this shouldn't compile on any major compiler," not "this isn't good functionality." I actually am curious why the spec doesn't have this functionality as part of the STL? – templatetypedef Jan 29 '11 at 01:29
  • @templatetypedef - You are right, I meant to write v[i], it will not compile with just v, and thanks, I changed the last bit of your for loop from ++V to i++ and it worked. Thank you! – UndefinedReference Jan 29 '11 at 01:31
  • @templatetypedef - I am as well. It seems mind-numbingly simple enough to implement in the generic case, with only one hiccup - `template std::ostream& operator<<(std::ostream &o, const std::vector& v) { std::copy(v.start(), v.end(), std::ostream_iterator(o, /* figure this part out and we're done */); return o; }` – Chris Lutz Jan 29 '11 at 01:32
  • @Chris Lutz: I've edited a corrected version of that into the answer, in case anybody wants it. At one time I wrote a more generic version that took various different container types, but I can't seem to find it at the moment... – Jerry Coffin Jan 29 '11 at 04:13
3

use srand(time(0)) for seeding, this way you will really get a pseudorandom number

pb2q
  • 58,613
  • 19
  • 146
  • 147
snoofkin
  • 8,725
  • 14
  • 49
  • 86
2

It looks like your program is only printing out one value:

cout << r << endl;

even though it looks like the given loop correctly generates the right number of random numbers. Are you sure that you're not creating the right number of numbers?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

move cout << r << endl; inside your loop, then it will display the rand number and continue in its loop.

pb2q
  • 58,613
  • 19
  • 146
  • 147