Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create random binary string: how to remove null character '\0' from to_string() before inserting to another string

I am trying to create a random binary string that consists of 0s and 1s. In my implementation i generate random integers 0s and 1s, and then I use std::to_string() in order to typecast them to a string and insert them into another string. The problem that I have is that it seems that by using std::to_string() in order to insert '0' or '1' characters, I also insert the terminating null character '\n' and therefore I double the size of the string. So for example, say I want to create a string consisting of Nbits=10 characters. With the implementation below I get a 10 elements string as printed on screen, however the size of the string is double that. Do you know how I could avoid 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 the correct one for the crossover/mutation operators to be correct.

#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 output:

1001111111
Number of bits is: 10, length of string is 20
like image 983
Foivos Avatar asked Sep 19 '26 15:09

Foivos


2 Answers

Your insertion logic is converting values it doesn't need to. There is no reason to convert data for a string of bits where you already know the possible outcomes of each bit: 0 or 1.

And .insert() is the wrong method. You're stacking data into a string that is already previously sized, thereby adding more chars, not replacing them. You should start with an empty string, then push data in, reserving if desired (but not required).

Try this:

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, exploit the fact that '0' and '1' are guaranteed to be contiguous values per the standard, and perhaps do something like this instead:

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 a multitude of ways to do this, just a few mentioned here. Best of luck.

like image 156
WhozCraig Avatar answered Sep 21 '26 04:09

WhozCraig


In the function random_string you create a string of Nbits characters, which means its size is Nbits. Then you insert characters, making the string longer.

There are two obvious solutions: One is to use i as an index into the string, and set that character. The other is to not set the size at all, and just append the new characters.

like image 31
Some programmer dude Avatar answered Sep 21 '26 06:09

Some programmer dude