Populating Vectors with Random Numbers

Hello!
So I am trying to populate 3 vectors with random numbers, but it keeps generating 51 as my random number and no others. I am not sure what I am doing wrong.

Thanks to anyone who helps in advanced!

#include<iostream>
#include<vector>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */

using namespace std;

int main(){
/*
NEED THIS COMMENT SECTION ON HOW TO RUN THE CODE
DESCRIPTION OF CONCEPTS
*/

//seeding srand
srand(time(0));

//generates random numbers from o to 99
int b = rand() % 100;

//First set of Vectors
vector<int> v1;
for (int i = 0; i <=5000; i++){
//to align up with time @ t=0
v1.push_back (b);
cout << v1[i] << endl;

}

vector<int> v2;
for (int j = 0; j <=10000; j++){
//to align up with time @ t=0
v2.push_back (b);
cout << v2[j] << endl;
}

vector<int> v3;
for (int k = 0; k <= 20000; k++){
//to align up with time @ t=0
v3.push_back (b);
cout << v2[k] << endl;
}

//Second set of Vectors, copy of first
vector<int> v4;
v4= v1;
for (int i = 0; i<= 5000; i++){
cout << v4[i] << endl;
}

vector<int> v5;
v5 = v2;
for (int j = 0; j <=10000; j++){
cout << v5[j] << endl;
}

vector<int> v6;
v6 = v3;
for (int k = 0; k <= 20000; k++){
cout << v6[k] << endl;
}

}
Last edited on
If you want multiple random numbers you need to call rand() multiple times. As it is now you're only generating one random number.

You probably want to move this line inside the loops.
 
int b = rand() % 100;
Using the random library and std::generate:
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
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include <algorithm>

constexpr auto low_bound = 0;
constexpr auto up_bound = 100;

int main()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//engine
    std::uniform_int_distribution<int> di(low_bound,up_bound);//distribution

    std::vector<int> v1(10);
    std::vector<int> v2(15);
    std::vector<int> v3(20);

    std::generate(v1.begin(), v1.end(), [&]{ return di(dre);});
    //http://en.cppreference.com/w/cpp/algorithm/generate
    std::generate(v2.begin(), v2.end(), [&]{ return di(dre);});
    std::generate(v3.begin(), v3.end(), [&]{ return di(dre);});

    std::cout << "V1: \n";
    for (const auto& elem : v1)std::cout << elem << " "; std::cout << "\n";

    std::cout << "V2: \n";
    for (const auto& elem : v2)std::cout << elem << " "; std::cout << "\n";

    std::cout << "V3: \n";
    for (const auto& elem : v3)std::cout << elem << " "; std::cout << "\n";
}


int b = rand() % 100;
should not be at :
1
2
3
4
int main()
{
int b = rand() % 100;
}


instead:


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
vector<int> v1;
for (int i = 0; i <=5000; i++)
{
srand(time(0)); 
int b = rand() % 100;  
v1.push_back (b);
cout << v1[i] << endl;

}

vector<int> v2;
for (int j = 0; j <=10000; j++)
{
srand(time(0)); 
int b = rand() % 100;
v2.push_back (b);
cout << v2[j] << endl;
}

vector<int> v3; 
for (int k = 0; k <= 20000; k++)
{
srand(time(0)); 
int b = rand() % 100;
v3.push_back (b);
cout << v2[k] << endl;
}



This calls the function rand() from the file #include<cstdlib>
3 individual times. Creating 3 different numbers between 0 and 99 and pushing them back into the first element of the 3 vectors.

You said
DESCRIPTION OF CONCEPTS
.
Well, here is the concept of SEEDING explained:

srand(time(0));
^
A computer does not generate random numbers, nor does it generate random numbers. That's right. In this specific universe, there is no such thing as "random".
A computer initially READS from a pre-determined LIST of numbers (Which I for the time being believe are incremented ascendingly), what your are doing when you are seeding the computer, you are telling the computer to begin reading from a specific place in said LIST, based on the current TIME and DATE of your PC - now, because every moment(Long integer value) is different on your PC, every time you begin the program, the PC will also start reading from somewhere else - giving you a different set of numbers to read from.

Last edited on
Topic archived. No new replies allowed.