• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ A question about c ++ programming

jackl90

Member
Joined
Jul 25, 2017
Messages
249
Reaction score
12
C++:
    bool updateLook = true;
    
    if(!hasFollowPath) {
    updateLook = false;
    }

How can I make the above function have 70% chance to be executed? To put one more condition together with !hasFollowPath


thank you all for your attention
 
Thanks for answer, so this is the correct form?
C++:
    if (uniform_random(1, 10) <= 7) {
        if(!hasFollowPath) {
        updateLook = false;
        }
    }
 
What do you think will happen faster, checking one bool or generating a random number and comparing its value? Order to change.
 
How is this related to what the OP is asking?
I guess he is asking which is faster:

C++:
    if (uniform_random(1, 10) <= 7) {
        if(!hasFollowPath) {
        updateLook = false;
        }
    }
or
C++:
    if(!hasFollowPath) {
        if (uniform_random(1, 10) <= 7) {
            updateLook = false;
        }
    }

In my personal opinion, checking a boolean is faster than generating the random and comparing it, but it won't gain almost any performance.
 
i think theys are talking about performance, which one is faster

C++:
    if (uniform_random(1, 10) <= 7) {
        if(!hasFollowPath) {
        updateLook = false;
        }
    }

or this


C++:
    if(!hasFollowPath) {
        if (uniform_random(1, 10) <= 7) {
            updateLook = false;
        }
    }

I have no experience, but the second is faster because have a bool first?
 
C++:
#include <iostream>
#include <chrono>
#include <cstdlib>
using namespace std;

int main()
{
    bool hasFollowPath = true;
    bool updateLook;

    auto t1 = std::chrono::high_resolution_clock::now();

    for(int i=0; i<1000; ++i){
        if(((std::rand() % 10 ) + 1) <= 7){
            if(!hasFollowPath){
                updateLook = false;
            }
        }
    }
    
    auto t2 = std::chrono::high_resolution_clock::now();
    auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
    std::cout << "Option #1: " << duration1 << "\n";
    
    auto t3 = std::chrono::high_resolution_clock::now();

    for(int i=0; i<1000; ++i){
        if(!hasFollowPath){
            if(((std::rand() % 10 ) + 1) <= 7){
                updateLook = false;
            }
        }
    }
    
    auto t4 = std::chrono::high_resolution_clock::now();
    auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 ).count();
    std::cout << "Option #2: " << duration2;

    return 0;
}
1570304428830.png
and with hasFollowPath = false:
1570304481282.png
Let us pay attention, even to details, so that we can create wonderful things.
 
@4drik
Unless you compiled without any optimization the whole check for hasFollowPath was optimized away.
But yes checking first for hasFollowPath is faster because mersenne twister algorithm sometimes need to generate new "pseudo random" values which is costly.
 
Back
Top