Generate a random binary string: how to remove the null character '\ 0' from to_string () before inserting into another string
I am trying to generate a random binary string composed of 0s and 1s. In my implementation, I generate random integers 0s and 1s and then I use std :: to_string () to cast them to a string and insert them into another string. The problem I am having is that when using std :: to_string () to insert the characters "0" or "1", I am also inserting the null character "\ n" and so I double the size of the string. For example, let's say I want to create a string consisting of Nbits = 10 characters. With the implementation below, I get a 10-element string printed on the screen, however the string is twice the size. Do you know how I could have avoided this?
The problem with the size is that I am trying to write a binary representation of a genetic algorithm and I need the size to be correct for the correct crossover / mutation operators.
#include <iostream>
#include <string>
#include <random>
using namespace std;
std::random_device rd;
std::mt19937 gen(rd());
// Random bit string generator
string random_string(size_t Nbits){
std::uniform_int_distribution<> int1(0,1);
string s;
s.resize(Nbits);
for(size_t i=0; i<Nbits; i++)
s.insert(i,to_string(int1(gen)));
return s;
};
int main(){
// Say I want a 10 bits random binary string
size_t Nbits=10;
string s=random_string(Nbits);
// If I print my string on screen, it has the correct number of entries:
cout<<s<<endl;
// However the size of the string is not equal to the number of entries.
cout<< "Number of bits is: "<< Nbits<<", length of string is "<< s.size()<<endl;
}
Possible way out:
1001111111
Number of bits is: 10, length of string is 20
source to share
Your insert logic will transform the values ββit doesn't need. There is no reason to convert data for a bit string where you already know the possible outcomes of each bit: 0
or 1
.
And .insert()
- the wrong method. You are adding data to a string that has already been given, thus adding more characters without replacing them. You should start with a blank line, then insert data, backing up if necessary (but not required).
Try the following:
std::string random_string(size_t Nbits)
{
std::uniform_int_distribution<> int1(0,1);
string s;
s.reserve(Nbits);
for (size_t i=0; i<Nbits; i++)
s.push_back(int1(gen) ? '1' : '0');
return s;
};
Alternatively, use the fact that '0'
and '1'
are guaranteed to be contiguous values ββby the standard, and perhaps do something like this:
std::string random_str(size_t Nbits)
{
std::string s;
std::generate_n(std::back_inserter(s), Nbits,
std::bind(std::uniform_int_distribution<char>('0', '1'),std::ref(gen)));
return s;
}
There are many ways to do this, only a few are mentioned here. Good luck.
source to share
In a function, random_string
you create a character string Nbits
, which means its size Nbits
. Then you insert characters, making the string longer.
There are two obvious solutions, one is to use it i
as an index in the string and set that character. Another is not to set the size at all and just add new characters.
source to share
Another solution for generating a random binary string would be to generate a random value first:
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 20);
int val = dis(gen);
Then, pass that random value as an input argument to the next number to a binary string converter:
template<typename T>
std::string to_binary_string(T const &val) {
return std::string(std::bitset<sizeof(T) * CHAR_BIT>(val).template to_string<char,std::string::traits_type,std::string::allocator_type>());
}
source to share