CPPPROGLIB Telegram 6125
🎲 std::random vs rand() — генерация случайных чисел

Старый rand() из Си имеет множество проблем: плохое качество, ограниченный диапазон, глобальное состояние. В C++11 появились современные генераторы из <random>.

Старый подход:
#include <cstdlib>
#include <ctime>

srand(time(nullptr)); // Предсказуемо!
int dice = rand() % 6 + 1; // Неравномерное распределение!
double prob = rand() / (double)RAND_MAX; // Плохая точность


🍒 Современный подход:
#include <random>

// Инициализация генератора (один раз)
std::random_device rd;
std::mt19937 gen(rd()); // Mersenne Twister

// Равномерное распределение [1, 6]
std::uniform_int_distribution dice(1, 6);
int roll = dice(gen);

// Вещественное [0.0, 1.0)
std::uniform_real_distribution prob(0.0, 1.0);
double p = prob(gen);

// Нормальное распределение
std::normal_distribution normal(0.0, 1.0);
double value = normal(gen);



✏️ Доступные распределения:

• uniform_int_distribution — целые числа
• uniform_real_distribution — вещественные
• normal_distribution — гауссово
• bernoulli_distribution — булево
• exponential_distribution, poisson_distribution и другие

Библиотека C/C++ разработчика

#буст
Please open Telegram to view this post
VIEW IN TELEGRAM
👍12🔥7🌚2👾1



tgoop.com/cppproglib/6125
Create:
Last Update:

🎲 std::random vs rand() — генерация случайных чисел

Старый rand() из Си имеет множество проблем: плохое качество, ограниченный диапазон, глобальное состояние. В C++11 появились современные генераторы из <random>.

Старый подход:

#include <cstdlib>
#include <ctime>

srand(time(nullptr)); // Предсказуемо!
int dice = rand() % 6 + 1; // Неравномерное распределение!
double prob = rand() / (double)RAND_MAX; // Плохая точность


🍒 Современный подход:
#include <random>

// Инициализация генератора (один раз)
std::random_device rd;
std::mt19937 gen(rd()); // Mersenne Twister

// Равномерное распределение [1, 6]
std::uniform_int_distribution dice(1, 6);
int roll = dice(gen);

// Вещественное [0.0, 1.0)
std::uniform_real_distribution prob(0.0, 1.0);
double p = prob(gen);

// Нормальное распределение
std::normal_distribution normal(0.0, 1.0);
double value = normal(gen);



✏️ Доступные распределения:

• uniform_int_distribution — целые числа
• uniform_real_distribution — вещественные
• normal_distribution — гауссово
• bernoulli_distribution — булево
• exponential_distribution, poisson_distribution и другие

Библиотека C/C++ разработчика

#буст

BY Библиотека C/C++ разработчика | cpp, boost, qt


Share with your friend now:
tgoop.com/cppproglib/6125

View MORE
Open in Telegram


Telegram News

Date: |

A few years ago, you had to use a special bot to run a poll on Telegram. Now you can easily do that yourself in two clicks. Hit the Menu icon and select “Create Poll.” Write your question and add up to 10 options. Running polls is a powerful strategy for getting feedback from your audience. If you’re considering the possibility of modifying your channel in any way, be sure to ask your subscribers’ opinions first. The imprisonment came as Telegram said it was "surprised" by claims that privacy commissioner Ada Chung Lai-ling is seeking to block the messaging app due to doxxing content targeting police and politicians. For crypto enthusiasts, there was the “gm” app, a self-described “meme app” which only allowed users to greet each other with “gm,” or “good morning,” a common acronym thrown around on Crypto Twitter and Discord. But the gm app was shut down back in September after a hacker reportedly gained access to user data. Hashtags are a fast way to find the correct information on social media. To put your content out there, be sure to add hashtags to each post. We have two intelligent tips to give you: Developing social channels based on exchanging a single message isn’t exactly new, of course. Back in 2014, the “Yo” app was launched with the sole purpose of enabling users to send each other the greeting “Yo.”
from us


Telegram Библиотека C/C++ разработчика | cpp, boost, qt
FROM American