Newbie
Newbie

Reputation: 165

random between variables in C++?

First of all, I am still a beginner at learning programming. So please pardon me!

If i were to have 4 boolean values, let's say

a[0] = true
a[1] = false
a[2] = false
a[3] = true

and I were to randomize ONLY true values, how am I suppose to random them?

For example, since a[0] and a[3] is true, I can only random number 0 OR 3. but random goes something like this

int random = rand() % 4 + 0;

So it will random from range 0 TO 3. How am I suppose to do that with only true values? Please guide me along, thank you!

Upvotes: 1

Views: 102

Answers (4)

Chris Drew
Chris Drew

Reputation: 15334

As others have pointed out the correct answer is to construct a list of of eligible values and pick randomly from that list:

size_t randomInt(size_t min, size_t max) {
  static std::random_device rd;
  static std::mt19937 eng(rd());
  std::uniform_int_distribution<size_t> dist(min,max);
  return dist(eng);
}

int a[] = {true, false, false, true};

std::vector<int> eligible;
for(size_t i = 0u; i != 4u; ++i) {
  if(a[i])
    eligible.push_back(i);
}

auto random = eligible[randomInt(0u, eligible.size() - 1u)];

But if, for some reason it is not possible to create a list of eligible values upfront, it is interesting to know that you can select randomly from a sequence of unknown length. So you could pick an item from the array in a single pass if you really need to:

size_t n = 0u;
size_t random = 0u;
for(size_t i = 0u; i != 4u; ++i) {
  if (a[i]) {
    if (randomInt(0u, n) == 0)
      random = i;
    ++n;
  }  
}

Upvotes: 0

Shomz
Shomz

Reputation: 37701

If I understand correctly, you only want to pick an index where the value is true.

Two ways to do it:

  • make an array with just the true index numbers and pick a random there
  • keep randomizing you existing one until you get the true value (not really nice)

So, in the first case you'd have something like:

b[0] = 0;
b[1] = 3;

and then, since there are 2 such elements, you'd modify your randomizer to:

int random = rand() % 2;

Upvotes: 0

Andreas Vennstr&#246;m
Andreas Vennstr&#246;m

Reputation: 715

This is a solution I managed to cook together. The program loops through the bool array and adds all indices that are true to a temporary array.

#include <vector>
#define ARRAY_SIZE 4

int main( char** argv, int argc )
{
    bool a[ ARRAY_SIZE ] = { true, false, false, true };

    std::vector< int > Available;
    for ( int i = 0; i < ARRAY_SIZE; ++i )
        if ( a[ i ] )
            Available.push_back( i );

    int random = Available[ rand() % static_cast< int >( Available.size() ) ];
}

Upvotes: 0

Wyzard
Wyzard

Reputation: 34573

For this specific case where there are only two possible values to be chosen, you can choose a random number and check whether it's even or odd:

int random = (rand() % 2) ? 0 : 3;

More generally, you can make a list of the values that are eligible to be chosen, and then choose a random element from that list:

std::vector<int> choices = { 0, 3, 9, 42 };
int random = choices[rand() % choices.size()];

Upvotes: 2

Related Questions