Hi,

I am trying to generate 2000 random 3 bit binary numbers. I am using the following code to do that but I am getting an error.

#include <bitset>
#include <stdio.h>
#include <cstdlib>
#include <strings.h>
#include <iostream>
#include <vector>
#include <time.h>

using namespace std;

int main()
{

for(int i=0; i<2000; i++)
{
cout<< bitset<3> (unsigned char ( (rand() ) ) <<"\n";
}
return 0;
}

Can someone tell me the right way to generate random binary numbers like

000
001
010
..

Thanks

Dani AI

Generated

A modern, robust solution is to generate integers in the range 0..7 with C++11's <random> and format them with std::bitset<3>. That avoids the common pitfalls seen here: the original post had a syntax error and no proper seeding, 's manual converter is a good illustration of how to build bits but used rand()%7 (off-by-one), and 's bitset approach is concise but still uses the old rand() API which can introduce bias.

#include <iostream>
#include <bitset>
#include <random>

int main() {
    std::mt19937 rng(std::random_device{}());      // seed once
    std::uniform_int_distribution<int> dist(0, 7); // inclusive 0..7
    for (int i = 0; i < 2000; ++i)
        std::cout << std::bitset<3>(dist(rng)) << '\n';
}

Notes and troubleshooting:

  • std::uniform_int_distribution yields an unbiased integer in the specified range; prefer it over rand()%N.
  • Seed the engine once (shown above). For reproducible output, replace the seed with a fixed constant.
  • std::bitset<3> guarantees three characters with leading zeros (e.g. "001").
  • For cryptographic needs, use platform crypto APIs rather than std::mt19937 or std::random_device (the latter may be nondeterministic only on some implementations).

This keeps output correct, reproducible when required, and avoids the off-by-one and syntax issues that caused errors in earlier posts.

Recommended Answers

All 4 Replies

Its very simple to do. All you need is one function that converts an integer to a 3 bit binary number. If fact, its so simple I will give it to you :)

#include <iostream>
#include <ctime>

using namespace std;

void toBin(int num, char *lpStr, int len) {
	lpStr[len] = 0;
	for (int i = len - 1, remainder; i >= 0; i--)
		lpStr[i] = '0' + (remainder = num % 2, num /= 2, remainder);
}

int main() {
	char bin[4];
	srand(time(NULL));
	for (int i = 0; i < 2000; ++i) {
		toBin(rand()%7, bin, 3);
		cout << bin << '\n';
	}
	cin.ignore();
    return 0;
}

Heres how to get the binary of an integer which is what the function toBin does.
Say you want the binary of 18.

18 % 2 = 0
9  % 2 = 1
4  % 2 = 0
2  % 2 = 0
1  % 2 = 1

bin = 10010

. . .
int main()
{
for(int i=0; i<2000; i++)
{
cout<< bitset<3> (unsigned char ( (rand() ) ) <<"\n";
}
return 0;
}
. . .

Hello,

your idea of using <bitset> is completely correct. You only need to make some improvements like in:

int main(){
  const int n=3, d=1<<n;
  for (int i=0;i<2000;i++)cout<<bitset<n>(rand()%d)<<endl;
  return 0;
}
/* Result
001
011
001
011
011
110
011
100
111
...
*/

Isn't this a whole lot simpler than that queer one, eh? :D

krs,
tesu

eh, thats the boring way ^.^

eh, thats the boring way ^.^

No, it's just a tit-for-tat response :icon_mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.