C++ Random Number Generator AKA STOP USING Rand()

preview_player
Показать описание
The C++ random library is one of the most robust and effective ways of generating random numbers in C++. In this video I'll show you how to generate random numbers using both the old C style interface using rand() and also the Modern C++ style using random engines and distributions

Give me money:

Below are affiliate links, I may earn something if you purchase the mentioned product or service linked.

📚 Recommended Books

💵 Get $100 in credits from Vultr with this link
Рекомендации по теме
Комментарии
Автор

Bro, you can't go out of your way and tell people to stop using some function that everyone uses if you're using light mode

RedStoneMatt
Автор

How many libraries do you need to make a random number generator?
C++: YES!

SamoshiBG
Автор

Back when I used to do C++ for uni projects, I had implemented two very simple texture generation algorithms for an OpenGL game we were making (I only worked on the algorithm side of things). Working with the random library was a delight. It's one of the very few things in C++ that don't have any major hidden pitfalls (for my personal usecases). Coding in C++ feels really empowering when the batteries-included-ness of the stdlib kicks in. I'm trying to migrate to Rust these days and it feels kinda bittersweet to leave C++ behind.

raianmr
Автор

I think this generator is absolutely not recommended to use normally. Random device is a "real" random number generator, which means that does not follow a certain algorithm, but rather takes some variables from the system that are actually random and gives a number back. However, it can be terribly slow (the term people use is "running out of entropy", which causes the system to wait for some time until more is somehow created) and I do not think the quality of the numbers is guaranteed.

Instead use it only for the seed of an actual pseudo random number generator, like std::mt19937, which is incredibly fast and the numbers are good for a ridiculous amount of time. An example would be:

std::random_device rd;
std::mt19937 generator(rd());
dist(0.0, 1.0);
std::cout << dist(generator) << "\n";

peligros
Автор

Looking at the output of your program, I can confirm that 1:41 was filmed exactly on Thursday, September 1st 2022 at 10:06:48 PM GMT.

cno
Автор

Btw, for users, who don't have access or can't use c++20 for some reason, use std::generate, for a random vector...

dibyojyotibhattacherjee
Автор

Straight to the point and zero useless bullshit, nice.

aenapoeka
Автор

this is way better than my solution, which was to make a deck of cards and then use 1+rand(time(0))%2 to basically get a coin flip, if its 1 it goes i cut it from the list and put it in the front if its 2 it gets cut from the list and put at the end, then i run that 200 times and get what seems to be random

kirestus
Автор

I am in C++ for 30 years and I think the C rand() function is just fine for some uses. Yes, it is "bad" in terms of true randomness, but it can be still useful from time to time for simple things, where puristic randomness is not needed.

kanardia
Автор

6:40 to use std::ranges::shuffle you need include <algorithm> not <ranges>.

Orionus
Автор

Thank you! Subscribed. Hope you one day do a vid on a random in a range but with a bias towards a value and maybe a way to control the strength of that bias

VoidloniXaarii
Автор

this makes alot of assumtion about the viewer knowledge.

itstheer
Автор

Ah, I wish I could use C++20, but the computing cluster I target my software to is stuck on GCC 8.3.0. std::ranges would be very helpful

PKua
Автор

Your C++ videos are amazing! They help me learn!

BasPower
Автор

Thank you, I just started learning C++ and holy sht rand() and srand suck.

BlueFireDudester
Автор

btw why not use an array instead of a vector, jw since u are already declaring the size of the vector

Vili
Автор

very informative video btw but quick question, how to set range of random numbers that are generated in an output lets say my random generator displayed a generated numbers of 78934561 but i want to set a range of only 4 numbers which is 7893. what line of code should i input?

choupapi
Автор

You said one should always use a random_device with a distribution, but why is that?

JakobKenda
Автор

Thanks for the video.
I am also getting the same number everytime, even using the exact same code shown in 3:25. The srand is deleted by this point, by which I understand that there is no need to update/change the seed. Thanks for your answer.

alejandronieto
Автор

Ok Help!!! Newbie here! Used the Uniform distribution for random generated number in a function, but I need that number for a "Nested For Loop"; for 1 to the (generated number). Getting a error code of no conversion possible to make the randomize number into a integer. How do I get the " dist(rd)" (generated number) converted to a number for use in a "for loop"??

Mykussus