0

I am generating a std::vector<bool> of random booleans.

The way I'm doing it so far is something like this:

   static std::mt19937 generator; 
   static std::uniform_int_distribution<int> distribution(0,1);
   std::vector<bool> vec(10, false);

   for(auto val : vec) val = distribution(generator);

Is there a better or more efficient way of doing this?

24n8
  • 1,898
  • 1
  • 12
  • 25
  • I mean if you *really* wanted you could probably just generate one int and then read a bit, shift a bit, read a bit, shift a bit... but would it be more efficient? That depends on how big this vector is going to be. If you're only making a vector of size 10, why do you care about efficiency? – scohe001 Jan 16 '20 at 17:27
  • 4
    "better" and even "efficient" are rather vague terms. Are you concerned about time, memory, something else? Did you measure your code and realize that it doesnt fit your needs? – 463035818_is_not_an_ai Jan 16 '20 at 17:28
  • 2
    FWIW I would abandon using `vector` and just use a `vector` or something similar. Yeah, you don't get the space savings but it's so much easier to work with and all the "standard" techniques will work. – NathanOliver Jan 16 '20 at 17:29
  • Also, before measuring efficiency, make sure you have enabled your compilers optimizer. Testing performance of debug builds is rather pointless. – Jesper Juhl Jan 16 '20 at 17:30
  • @scohe001 Sorry, the 10 was just a random example, when it'll be user defined in my actual application, and can become quite large. – 24n8 Jan 16 '20 at 17:32
  • @formerlyknownas_463035818 I am concerned, but not that concerned. I think my question should instead have been "Is there something drastically better than what I'm doing?" I'm new to using the random libraries in C++, so not sure if I was doing something ludicrous for something simple. – 24n8 Jan 16 '20 at 17:34
  • 2
    The biggest issue with what you have done is you did not seed `generator`. If you don't seed it you'll always get the same results. – NathanOliver Jan 16 '20 at 17:35
  • you could intialize the vector with random entries instead of first initializing with `false` and then assigning randoms, but really for a vector with 10 entries it wont make a noticable difference – 463035818_is_not_an_ai Jan 16 '20 at 17:36
  • Why not [`std::bernoulli_distribution`](https://en.cppreference.com/w/cpp/numeric/random/bernoulli_distribution)? – Evg Jan 16 '20 at 17:41
  • @NathanOliver Thanks for catching that! – 24n8 Jan 16 '20 at 17:59
  • @Evg Thanks. I was not aware that this was already implemented in `std`. What's the benefit of using this vs. what I have? Not having to do an implicit conversion from int to bool? – 24n8 Jan 16 '20 at 18:01
  • @formerlyknownas_463035818 How do you initialize a vector with random entries? I don't think I also a constructor that has that capability? – 24n8 Jan 16 '20 at 18:29
  • For me the main benefit is more clear expression of the intent. – Evg Jan 16 '20 at 18:30
  • 1
    There is a constructor that takes a pair of iterators. You can use it to avoid redundant initialization. [Example](https://godbolt.org/z/JvvRAP). – Evg Jan 16 '20 at 18:49
  • 2
    You might be interested by result from [c-performance-for-drawing-random-boolean-values](https://stackoverflow.com/questions/57227028/c-performance-for-drawing-random-boolean-values). There is also – Jarod42 Jan 16 '20 at 18:57
  • @NathanOliver Do you mean if you DO seed it, you will always get the same results? – user5965026 Apr 24 '20 at 17:19
  • @user5965026 No, I said if you don't seed `generator`, you'll always get the same results. A default constructed `std::mt19937` is guaranteed to produce the same results. – NathanOliver Apr 24 '20 at 17:43
  • @NathanOliver But if you do provide a seed, don't you get the same results? Like say you have a program generating 100 random values. If you set a seed, then each time you run that program you get the same sequence of 100 values? – user5965026 Apr 24 '20 at 18:24
  • Well yes, you have to seed it correctly. Normally the time is used since it changes every time the program is ran. – NathanOliver Apr 24 '20 at 18:55

0 Answers0