random generator number

according to this random generator number code:

srand(time(0));
for (i = 1; i <= 26; i++)
{
bufferData.push_back(i);
}
for (i = 1; i <= bufferData.size(); i++)
{
bufferData[i] = rand() % 2;
std::cout << bufferData[i] % 2 << " ";
}

is it possible to write it in one loop and statement?
//khashayar xcm fallen

#include <stdio.h> //or #include<cstdio>

void main (int argc ,char *argv[]){

int _rand= 0;

_rand= rand() % 10 + 1; //generate number between 1 and 10
printf("number is %d",_rand); //print the number

}
closed account (E0p9LyTq)
Doing it all in one loop eazy peazy (quick and dirty without a lot of error-checking):

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

int main()
{
   // seed the C random generator ONCE
   srand(time(nullptr));

   // number of items needed for vector
   const int num_items = 26;
   
   // create the vector
   std::vector<int> bufferData;
   
   // reserve space for num_items elements
   bufferData.reserve(num_items);

   for (int loop = 0; loop < num_items; loop++)
   {
      // generate a random number between 0 & 1
      int temp = rand() % 2;

      // add the random number to the vector
      bufferData[loop] = temp;
      
      // display the current vector element
      std::cout << bufferData[loop] << ' ';
   }
   std::cout << '\n';
}

Vectors are ZERO-BASED containers, your loop will read past the end of the vector.

Using the C++ random library:

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
#include <iostream>
#include <vector>
#include <random>

int main()
{
   // create a random engine and seed using the random device
   std::random_device         rd;
   std::default_random_engine rng(rd());

   // create a distribution
   std::uniform_int_distribution<> dist(0, 1);

   // number of elements needed for the vector
   const int num_items = 26;

   // create a vector of size 'num_items' and initialize all elements to zero
   std::vector<int> bufferData = { 0 };
   
   // reserve space for num_items elements
   bufferData.reserve(num_items);

   for (int loop = 0; loop < num_items; loop++)
   {
      // generate a random number between 0 & 1 and add it to the vector
      bufferData[loop] = dist(rng);

      // display the current vector element
      std::cout << bufferData[loop] << ' ';
   }
   std::cout << '\n';
}


PLEASE learn to use code tags, it makes reading your source code MUCH easier.
http://www.cplusplus.com/articles/jEywvCM9/

You can edit your post and add code tags.
Last edited on
You could also use std::generate (or std::generate_n).

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
34
35
36
37
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <random>
#include <chrono>
#include <algorithm>
using namespace std;


// RNG and probability distribution
mt19937 gen( chrono::system_clock::now().time_since_epoch().count() );
uniform_int_distribution<int> dist( 0, 99 );

// Generator functions supplying random numbers
int oldRand() { return rand() % 100; }
int newRand() { return dist( gen ); }


int main()
{
   srand( time( 0 ) );
   const int N = 10;
   int data[N];


   generate( data, data + N, oldRand );
   cout << "Using rand()  : ";
   for ( int i : data ) cout << setw( 2 ) << i << ' ';
   cout << '\n';


   generate( data, data + N, newRand );
   cout << "Using <random>: ";
   for ( int i : data ) cout << setw( 2 ) << i << ' ';
   cout << '\n';
}

Using rand()  : 34 58 36 98 17 21 72 45 64  2 
Using <random>: 35 71 61  8 86 77 23 91  0 52 
Topic archived. No new replies allowed.