Random Number generation missing a pattern for the same seed - c++

I am generating a random number for a particular seed ...So I have a property called seed which I allow the user to set. I give him two options: reset and trigger. The reset will re start the generation of random numbers. And trigger will generate the next random number. The pseudo code is somewhat like
::setSeed( unsigned & seed )
{
m_seed = seed
}
::reset()
{
m_seed = getcurrentseed()
srand(m_seed);
}
::trigger()
{
raValue = minValue + ( rand() % (maxValue-minValue+1) );
}
For a particular seed if I generate say 5 random values 5 times ..sometimes I see a value missing in one of the sets . What could be the reason ?
For eg.
seed(5)
rand()
rand()
rand()
seed(5)
rand()
rand()
rand()
After the second seed(5) call, sometimes I get a different sequence of numbers or I miss a number from the previous sequence
void RandomNodeLogic::process( SingleInputValueGetters const& singleInputValueGetters
, MultiInputValueGetters const& /*multiInputValueGetters*/
, OutputValueKeepers const& /*outputValueKeepers */
, OutputValueSetters const& outputValueSetters )
{
// get the seed and generate the random number
bool doRandom( false );
int maxValue= getMaxValue().getAs<int>();
int minValue = getMinValue().getAs<int>();
int newValue=0;
if(minValue > maxValue)
{
setMaxValue(minValue);
setMinValue(maxValue);
maxValue= getMaxValue().getAs<int>();
minValue = getMinValue().getAs<int>();
}
SingleInputValueGetters::const_iterator it = singleInputValueGetters.begin();
for( ; it != singleInputValueGetters.end(); ++it)
{
SlotID id = it->first;
const SlotValue* value = it->second();
if(!value)
{
continue;
}
if ( id == RTT::LogicNetwork::RandomNode::nextValuesSlotID )
{
doRandom = value->getAs<bool>();
newValue = minValue + ( rand() % (maxValue-minValue+1) ); // read the value from the next input slot
setRandomValue( ::convertToSlotValue( m_genValue.m_attrType, newValue ) );
}
else if ( id == RTT::LogicNetwork::RandomNode::resetValuesSlotID )
{
if ( value->getAs<bool>() )
{
doRandom = value->getAs<bool>();
setSeed(m_seed);
newValue = minValue + ( rand() % (maxValue-minValue+1) );
setRandomValue( ::convertToSlotValue( m_genValue.m_attrType, newValue ) );
}
}
}
if(!m_genValue.empty() && doRandom)
{
outputValueSetters.find( RTT::LogicNetwork::RandomNode::outputValuesSlotID)->second( m_genValue.getAs<int>() ) ;
RTT_LOG_INFO( QString("Random Number: %1").arg( m_genValue.getAs<int>() ));
}
if(!doRandom)
{
if(m_genValue.empty())
{
srand(1);
m_genValue = 0;
}
getAssociatedNode()->sleep();
}
}
void RandomNodeLogic::setSeed( const SlotValue& seed )
{
SlotValue oldValue = m_seed;
m_seed = seed;
srand(m_seed.getAs<unsigned int>());
modifiablePropertyValueChanged( RTT::LogicNetwork::RandomNode::seedPropertyID, m_seed, oldValue );
}

All informations you gave so far tell me that you are trying to write a C++ class wrapper for C random functions. Even if this wrapper is a class that can be instantiated multiple times, the underlying C functions access only one state. That's why you have to ensure that nobody else is using an instance of this wrapper class.
That's why it is a bad idea, to wrap C's random functions with a C++ class. As an C++ exercise in random try to implement your own random class, it's not as hard as it seems.

Related

Global variable doesn't update prior to next loop

I'm trying to build a tachometer in C++ for my ESP32. When I uncomment Serial.printf("outside rev: %d \n", rev); outside of the conditional it works, but when I comment it I get values that are orders of magnitude greater than they should be (700 revolutions without, vs 7 revolutions with). My best guess is that the print statement is slowing the loop() down just enough for incrementRevolutions() to toggle the global variable passedMagnet from true to false before the next loop. That would make sense, since a delay in updating passedMagnet would allow newRevCount++; to be triggered multiple times. But this is obviously something I can't debug with either print statements or step-through debugging given the time-sensitive nature of the race condition.
bool passedMagnet = true;
int incrementRevolutions(int runningRevCount, bool passingMagnet)
{
// Serial.printf("passedMagnet: %d , passingMagnet %d , runningRevCount: %d \n", passedMagnet, passingMagnet, runningRevCount);
int newRevCount = runningRevCount;
if (passedMagnet && passingMagnet)
{ //Started a new pass of the magnet
passedMagnet = false;
newRevCount++;
}
else if (!passedMagnet && !passingMagnet)
{ //The new pass of the magnet is complete
passedMagnet = true;
}
return newRevCount;
}
unsigned long elapsedTime = 0;
unsigned long intervalTime = 0;
int rev = 0;
void loop()
{
intervalTime = millis() - elapsedTime;
rev = incrementRevolutions(rev, digitalRead(digitalPin));
// Serial.printf("outside rev: %d \n", rev);
if (intervalTime > 1000)
{
Serial.printf("rev: %d \n", rev);
rev = 0;
elapsedTime = millis();
}
}
Is this a known gotcha with Arduino or C++ programming? What should I do to fix it?
I think the test is to blame. I had to rename and move things a bit to visualize the logic, sorry about that.
bool magStateOld = false; // initialize to digitalRead(digitalPin) in setup()
int incrementRevolutions(int runningRevCount, bool magState)
{
int newRevCount = runningRevCount;
// detect positive edge.
if (magState && !magStateOld) // <- was eq. to if (magState && magStateOld)
// the large counts came from here.
{
newRevCount++;
}
magStateOld = magState; // record last state unconditionally
return newRevCount;
}
You could also write it as...
int incrementRevolutions(int n, bool magState)
{
n += (magState && !magStateOld);
magStateOld = magState;
return n;
}
But the most economical (and fastest) way of doing what you want would be:
bool magStateOld;
inline bool positiveEdge(bool state, bool& oldState)
{
bool result = (state && !oldState);
oldState = state;
return result;
}
void setup()
{
// ...
magStateOld = digitalRead(digitalPin);
}
void loop()
{
// ...
rev += (int)positiveEdge(digitalRead(digitalPin), magStateOld);
// ...
}
It's reusable, and saves both stack space and unnecessary assignments.
If you cannot get clean transitions from your sensor (noise on positive and negative edges, you'll need to debounce the signal a bit, using a timer.
Example:
constexpr byte debounce_delay = 50; // ms, you may want to play with
// this value, smaller is better.
// but must be high enough to
// avoid issues on expected
// RPM range.
// 50 ms is on the high side.
byte debounce_timestamp; // byte is large enough for delays
// up to 255ms.
// ...
void loop()
{
// ...
byte now = (byte)millis();
if (now - debounce_timestamp >= debounce_delay)
{
debounce_timestamp = now;
rev += (int)positiveEdge(digitalRead(digitalPin), magStateOld);
}
// ...
}

c++ find and handle each lines is slower than php

Hello i created a program to handle a config file line by checking each lines and get the config blocks but for first time i made it with php and the speed was amazing. we have some blocks like this
Block {
}
php program can read each line and detect about 50,000 of this blocks in just 1 second after that i went to c++ to create my program in c++ but i saw a very very bad problem. my program was too slow (read 50,000 of this blocks in 55 seconds) while my php codes was exactly the same of c++ codes (in action and activity). php was 55x faster than c++ while the codes are the same.
this is my code in php
const PATH = "conf.txt";
if(!file_exists(PATH)) die("path_not_found");
if(!is_readable((PATH))) die("path_not_readable");
$Lines = explode("\r\n", file_get_contents(PATH));
class Block
{
public $Name;
public $Keys = array();
public $Blocks = array();
}
function Handle(& $Lines, $Start, & $Return_block, & $End_on)
{
for ($i = $Start; $i < count($Lines); $i++)
{
while (trim($Lines[$i]) != "")
{
$Pos1 = strpos($Lines[$i], "{");
$Pos2 = strpos($Lines[$i], "}");
if($Pos1 !== false && ($Pos2 === false || $Pos2 > $Pos1)) // Detect { in less position
{
$thisBlock = new Block();
$thisBlock->Name = trim(substr($Lines[$i], 0, $Pos1));
$Lines[$i] = substr($Lines[$i], $Pos1 + 1);
Handle($Lines, $i, $thisBlock, $i);
$Return_block->Blocks[] = $thisBlock;
}
else { // Detect } in less position than {
$Lines[$i] = substr($Lines[$i], $Pos2 + 1);
$End_on = $i;
return;
}
}
}
}
$DefaultBlock = new Block();
Handle($Lines, 0, $DefaultBlock, $NullValue);
$OutsideKeys = $DefaultBlock->Keys;
$Blocks = $DefaultBlock->Blocks;
echo "Found (".count($OutsideKeys).") keys and (".count($Blocks).") blocks.<br><br>";
and this is my code in C++
string Trim(string & s)
{
auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) {return std::isspace(c); });
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) {return std::isspace(c); }).base();
return (wsback <= wsfront ? std::string() : std::string(wsfront, wsback));
}
class Block
{
private:
string Name;
vector <Block> Blocks;
public:
void Add(Block & thisBlock) { Blocks.push_back(thisBlock); }
Block(string Getname = string()) { Name = Getname; }
int Count() { return Blocks.size(); }
};
void Handle(vector <string> & Lines, size_t Start, Block & Return, size_t & LastPoint, bool CheckEnd = true)
{
for (size_t i = Start; i < Lines.size(); i++)
{
while (Trim(Lines[i]) != "")
{
size_t Pos1 = Lines[i].find("{");
size_t Pos2 = Lines[i].find("}");
if (Pos1 != string::npos && (Pos2 == string::npos || Pos1 < Pos2)) // Found {
{
string Name = Trim(Lines[i].substr(0, Pos1));
Block newBlock = Block(Name);
Lines[i] = Lines[i].substr(Pos1 + 1);
Handle(Lines, i, newBlock, i);
Return.Add(newBlock);
}
else { // Found }
Lines[i] = Lines[i].substr(Pos2 + 1);
return;
}
}
}
}
int main()
{
string Cont;
___PATH::GetFileContent("D:\\conf.txt", Cont);
vector <string> Lines = ___String::StringSplit(Cont, "\r\n");
Block Return;
size_t Temp;
// The problem (low handle speed) start from here not from including or split
Handle(Lines, 0, Return, Temp);
cout << "Is(" << Return.Count() << ")" << endl;
return 0;
}
as you can see, this codes are exactly the same in action but i don't know why php handling in this code is 55x faster than my c++ codes. you can create a txt file and create about 50,000 of this block's
Block {
}
and test it yourself. please help me to fix this. i am really confused (same codes but not same performance
php = 50,000 blocks and detect in 1 second
c++ = 50,000 blocks and detect in 55 seconds (and maybe more) !
i have no problem in my program design. because i got my performance completely on php but my problem is on c++ that is 55x slower than php in same code action !
i am using (visual studio 2017) to compile this program (c++)
First, "code" is singular, not plural.
C++ is a very different language than php. It is not "the same code", and it is nowhere near the same in action.
For example, these two lines:
Block newBlock = Block(Name);
Return.Add(newBlock);
First create a Block on the stack, and then call Block's copy constructor to make another one inside the vector. You then throw away the stack object.
Also, vectors guarantee that they are contiguous, so as you add new Blocks via your Add method, vector will occasionally stop, allocate another chunk of memory (twice as big as the last one, iirc), copy everything over to that new chunk, and then free the old one. Either preallocate the vector (via vector::reserve()), or consider using something like a deque that doesn't guarantee continuity in memory if you don't need that property.
I also don't know what ___String::StringSplit does, but you are almost certain to have the same vector growth problem in reading your file.
Culprit is in these 2 lines:
Handle(Lines, i, newBlock, i);
Return.Add(newBlock);
Let's say you have 5 levels of 1 block each. What Happens on bottom one? You copy one instance of block. What happens on level 4? You copy 2 blocks (parent and its child). So for level 5 you make 15 copies - 1+2+3+4+5. Look at this diagram:
Handle level1 copies 5 blocks (`Return`->level4->level3->level4->level5)
Handle level2 copies 4 blocks (`Return`->level3->level4->level5)
Handle level3 copies 3 blocks (`Return`->level4->level5
Handle level4 copies 2 blocks (`Return`->level5)
Handle level5 copies 1 block (`Return`)
Formula is:
S = ( N + N^2 ) / 2
so for levels 20 you would do 210 copies and so on.
Suggestion is to use move semantics to avoid this copy:
// change method Add to this
void Add(Block thisBlock) { Blocks.push_back(std::move(thisBlock)); }
// and change this call
Return.Add( std::move( newBlock ) );
Or allocate blocks dynamically using smart pointers
Out of simple curiousity, try this Trim implementation instead:
void _Trim(std::string& result, const std::string& s) {
const auto* ptr = s.data();
const auto* left = ptr;
const auto* end = s.data() + s.size();
while (ptr < end && std::isspace(*ptr)) {
++ptr;
}
if (ptr == end) {
result = "";
return;
}
left = ptr;
while (end > left && std::isspace(*(end-1))) {
--end;
}
result = std::string(left, end);
}
std::string Trim(const std::string& s) {
// Not sure if RVO would fire for direct implementation of _Trim here
std::string result;
_Trim(result, s);
return result;
}
And another optimization:
void Add(Block& thisBlock) {
Blocks.push_back(std::move(thisBlock));
}
// Don't use thisBlock after call to this function. It is
// far from being pretty but it should avoid *lots* of copies.
I wonder if you'll get better result. Pls let me know.

Which is the best constructor of unordered_map to replace one old project defined class UtilHashTable

In our base code, we are trying to replace one project defined class UtilHashTable by unordered_map to improve performance.
C'tor of UtilHashTable looks like:
template< class Key, class Val, class Hash >
UtilHashTable< Key, Val, Hash >::UtilHashTable( unsigned inInitialBuckets,
unsigned inMaxProbes ) :
cMaxProbes( inMaxProbes )
{
cLoadFactor = 0.5;
mSize = 0;
unsigned size = 1;
while( inInitialBuckets > 0 )
{
size <<= 1;
inInitialBuckets >>= 1;
}
//mTable = new vector< Entry >( size );
mTable = new unordered_map< Key, Val >( size );
mNumRehashes = 0;
mNumLookups = 0;
mNumProbes = 0;
mHashMask = static_cast< uint32>( mTable->size() - 1 );
}
In the header file, its declaration looks like:
UtilHashTable( unsigned inInitialBuckets = 500, unsigned inMaxProbes = 40 );
We are providing flexibility to the user to set InitialBuckets and MaxProbes if they want!
Please suggest me which c'tor of unordered_map will work best to replace everything inside the c'tor of UtilHashTable.
Tell me if you need any further info.
Thanks in advance! :D

optimize octree octant_determination function in c++

I am building a spacial octree. In order to determine in which branch/octant a certain point (x,y,z) should be placed, I use this function:
if (x>x_centre) {
xsign = 1;
}
else {
xsign = 0;
}
if (y>y_centre) {
ysign = 1;
}
else {
ysign = 0;
}
if (z>z_centre) {
zsign = 1;
}
else {
zsign = 0;
}
return xsign + 2*ysign + 4*zsign;
It returns a number between 0 and 7 unique for every octant. It turns out this snippet is called a big many times. It gets quite time consuming when building large trees.
Is there any easy way to speed this proces up?
This allready gives a 30 percent speed up:
xsign = x>x_centre;
ysign = y>y_centre;
zsign = z>y_centre;
return xsign + 2*ysign + 4*zsign;
Any other tips?

Random generator test. Where is error?

Strange tests behaviour.
I have class that generate random values.
std::random_device RandomProvider::rd;
std::mt19937 RandomProvider::rnb(RandomProvider::rd());
#define mainDataType unsigned int
mainDataType RandomProvider::GetNextValue(mainDataType upperLimit)
{
static std::uniform_int_distribution<int> uniform_dist(1, upperLimit);
return uniform_dist(rnb);
}
And unit-test that test it's behavior.
TEST_METHOD(TestRandomNumber)
{
CreateOper(RandomNumber);
int one = 0, two = 0, three = 0, unlim = 0;
const int cycles = 10000;
for (int i = 0; i < cycles; i++)
{
mainDataType res = RandomProvider::GetNextValue(3);
if (res == 1) one++;
if (res == 2) two++;
if (res == 3) three++;
}
double onePerc = one / (double)cycles;
double twoPerc = two / (double)cycles;
double threePerc = three / (double)cycles;
Assert::IsTrue(onePerc > 0.20 && onePerc < 0.40);
Assert::IsTrue(twoPerc > 0.20 && twoPerc < 0.40);
Assert::IsTrue(threePerc > 0.20 && threePerc < 0.40);
}
Test passed all-times in debug and if i chose it and Run only it. But it fails all times when i
run it with other tests. I added debug output to text file and got unreal values onePerc = 0.0556, twoPerc= 0.0474 and threePerc = 0.0526... What is going on here? (i am using VS2013 RC)
Since you use a static uniform_int_distribution the first time you call GetNextValue the max limit is set, never being changed in any subsequent call. Presumably in the test case you mentioned, your first call to GetNextValue had a different value than 3. Judging from the values returned it looks like probably either 19 or 20 was used in the first such call.