// this program demonstrates experimentally than when we AND together any number of inequalities (>0)
// we select a single range which may be open on one end only, or closed; or we select nothing
#include <iostream>
#include <random>
#include <functional>
#include <map>
#include <sstream>
int main()
{
std::default_random_engine generator(time(0)/*seed*/);
std::geometric_distribution<int> terms_dist(0.3);
std::uniform_int_distribution<int> operation_dist(0, 3); // <, >, <=, >=
std::uniform_int_distribution<int> value_dist(-10, 10);
for(int i=0;i<1000;i++)
{
std::vector<std::function<bool(int)> > and_terms;
auto num_inequalities = terms_dist(generator) + 1; // more usually a low integer of at least 1
// and_terms will be a list of functions that test a parameter against a certain random operation and value
std::ostringstream description;
for(int j=0; j<num_inequalities; j++)
{
auto op = operation_dist(generator);
auto val = value_dist(generator);
description << (j?" and ":" ");
switch(op)
{
case 0: description << "<" << val; break;
case 1: description << ">" << val; break;
case 2: description << "<=" << val; break;
case 3: description << ">=" << val; break;
}
and_terms.emplace_back( [op, val](int v)
{
switch(op)
{
case 0: return v < val;
case 1: return v > val;
case 2: return v <= val;
case 3: return v >= val;
}
return false; // unreachable
} );
}
// perform tests on range [-11,11]
std::map<int,bool> results;
for(int v=-11; v<=11; v++)
{
results[v]=true; // assume they all passed
for(int j=0; j<num_inequalities; j++)
if(!and_terms[j](v))
{
results[v]=false;
break; // short circuit
}
if(results[v])
std::cout << "*";
else
std::cout << ".";
}
std::cout << description.str() << std::endl;
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: