Reputation: 209
I'm trying to automate test cases for my program and i wanted to generate random strings of numbers to test with... First test will be with base 2 numbers, so i want to generate 0's and 1's, of a random length n, where n is from 1 to 100. This is what i have so far, and it keeps returning the same string, (same length) and only generates either a 1, or a 0 for the entire length of the string.
int digNum = rand() % 100 + 1;
int num;
for(int i = 0; i < digNum; i++) {
num = ((double) rand() / (RAND_MAX)) + 1;
s1 = s1 + to_string(num);
}
cout << s1 << endl;
Thoughts?
Upvotes: 3
Views: 2880
Reputation: 10393
You can use std::uniform_int_distribution
to generate integer values.
#include <random>
#include <string>
template <typename Generator>
std::string random_binary_string(Generator& g, std::size_t n) {
std::uniform_int_distribution<char> d('0', '1');
std::string s;
s.reserve(n);
for (std::size_t i = 0; i < n; ++i) {
s += d(g);
}
return s;
}
template <typename Generator>
std::size_t random_size(Generator& g, std::size_t a, std::size_t b) {
std::uniform_int_distribution<std::size_t> d(a, b);
return d(g);
}
A std::random_device
will usually generate non-deterministic random numbers.
#include <iostream>
int main() {
std::random_device g;
std::cout << random_binary_string(g, random_size(g, 1, 100)) << std::endl;
}
Upvotes: 1
Reputation: 173
For the 0/1 problem :
num = rand()%2;
Will generate a number that is 0 or 1 more simply. With your current expression, rand()/RAND_MAX is always <1, unless rand()==RAND_MAX, in which case it equals 1 . Adding 1 gives almost always 1, or 2 if rand()==RND_MAX
For the constant result
Even if rand() returns a pseudo random sequence, it will always generate the same sequence at program start by default. The result will be different if you call it a second time in the same program, as you advanced in the random number sequence. If you want the first calls to rand() of each proces run tu return different results, you have to call
srand(time(NULL));
before calling rand()
Upvotes: 1
Reputation: 11
If you divide srand() function by RAND_MAX you get a number in 0..1, and by casting to integer you get in lot of cases 0 (due to truncation). And finally 0 + 1 is 1.
Maybe you need something like this:
rand() % (base) i.e. rand() % (2)
Don't forget to initialize random number generator:
srand(time(NULL));
Upvotes: 1
Reputation: 188
First you should initialize generator, e.g:
srand(time(NULL));
With this you will make sure you won't get the same numbers every time.
And in order to generate numbers from range you should try:
rand() % (max + 1 - min) + min
Upvotes: 3
Reputation: 236
Alternatively, you could use <random>
to generate your strings if you have C++11 or higher.
#include <iostream>
#include <chrono>
#include <random>
#include <algorithm>
int genRandom(std::mt19937 & mt, int ls, int rs)
{
std::uniform_int_distribution<int> distribution(ls, rs);
return distribution(mt);
}
int main()
{
std::mt19937 mt_rand;
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
mt_rand.seed(static_cast<unsigned>(seed));
std::string randomString(genRandom(mt_rand, 0, 100), ' ');
std::generate(std::begin(randomString), std::end(randomString), [&] () { return '0' + (genRandom(mt_rand, 0, 1)); });
std::cout << randomString << std::endl;
return 0;
}
Upvotes: 4