Похожие чаты

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 ответов

11 просмотров

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.

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

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

Сообщение* в закодированном виде. То есть, просто сделать sendMessage?text=Привет бла-бла! не получится, надо в HEX переводить, и добавлять процент, типа такого: sendMessage?t...
КТ315
21
А случайно нет ли в паскале штатной возможности передать указатель и количество туда где array of в качестве аргумента?
zamtmn
25
@LGCYMan @JustinKKW Q/A List 1. this part confuses me on #8: "From there, a bridge between erc20 LGXY and erc20 LGXY will be out once that swap is closed." does this mean ...
Sig | LGCY | BattleVerse L1 | BitWin
1
Всем привет. Испытываю проблемы в работе БД, а именно огромного роста логов, такого характера: 024-05-16 18:39:07 +05 sentry sentry [unknown] 1050169 7-1 app-sentry01.corp.ru>...
Alexey
2
Anyone have any good learning material on how to do network analysis on cryptocurrency? Obviously monero is different story, but for Ethereum network for example. Thanks.
John Doe
13
Anyone here suffers from unexplained aural migraines, who would be up for talking for a bit? Doesn't *have* to be aural, but I am not asking about headaches, I mean actual mi...
Martin Rys
58
Хм. А телеграм апи работают через HTTP?
The Bird of Hermes
14
Did @Beeker really get banned? That's ridiculous. He's one of the few left here with half a brain and you might not always like his narrative @dgb_ycagel , but he often keeps ...
Leroy Gerrits
1
Почему Telegram пишет, что объект media не найден, хотя на самом деле я его передаю? Делаю на urllib, без зависимостей, так надо. Вызываю метод sendMediaGroup с таким JSON: ...
Alexey S
1
Ну раз я пока тут, задам пару глупых вопросов. Зачем писать на ассемблере если компилятор довольно умный, а ассемблер много времени занимает? В каких прикладных задачах сейчас...
Максим Рябцев
20
Карта сайта