Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

Random number generator with one for-loop

$
0
0
According to the following code which can generate random binary numbers, how I can decrease these two for-loops to one for-loop?

Code:

#include <iostream>
#include <string>
#include <ctime>
#include <vector>
using namespace std;

int main()
{
  std::vector<int> b;
  srand(time(0));
  for (int i = 1; i <= 11; i++)
  {
    b.push_back(i);
  }
  for (int i = 1; i <= b.size(); i++)
  {
  b[i] = rand() % 2;
  std::cout << b[i] % 2 << " ";
  }
}


Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images