Похожие чаты

Hey guys, any suggestion regarding this: int n = rand() %

2;
if (n==0)
{
doSomthing()
}

doSomething() function will be executed at a probable (random) intervals based on this condition. However, any advice on how to make it more stable? Meaning to have a condition that will execute doSomthing() function exactly 50% or 30% of the whole run time.

8 ответов

26 просмотров

U can use the rand function itself

n = rand() % 10; if (n > 4) {...} that will be for 50% theoretically, :)

Kuntal M
n = rand() % 10; if (n > 4) {...} that will be fo...

n % 2 == 0, it will work even more precise :)

Monem- Автор вопроса

Everyone just uses modulus like that, but everyone ponders how to do better too... The usual attempt looks something like: #define N 100 // or whatever #define USE_MAX (N * (RAND_MAX/N)) // re-multiply and idiv truncate int x; do x = rand(); while(x >= USE_MAX); x %= N; I believe there is a better way but don't know off the top of my head.

Monem- Автор вопроса

If you're using C++ you'll better results using uniform https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution With values, for example 1,100 and probability to get value, for example < 20 will be 20% That makes it very easy to change probabilities when you need.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта