Create a Random Number in C++

preview_player
Показать описание
Demonstrates how to use rand() and srand() to generate random numbers. The software used in this tutorial is Xcode, but the code can be applied to any C++ compiler.
Table of Contents:

00:26 - rand() function
01:11 - cstdlib header file
01:49 - Pseudorandom Number
02:39 - time() function
03:18 - ctime header file
03:32 - srand() function
Рекомендации по теме
Комментарии
Автор

finally, someone who could explain how this function works!

AtifShafiinheritance
Автор

thank you, you are the only one who could make a random number change in greeks for greeks and code greeper (in my search)

guninchoudhary
Автор

Finally understand what srand means in c++ thanks a lot

amanvishnoi
Автор

very simple, but the explanation was very clear. thanks!!

meove
Автор

finally an explanation that make sense about srand()..thank you

wisamkhalid
Автор

DO NOT use '%' to generate random numbers except when you know that rand_max is exactly divisible by the operand. For instance, if rand_max = 20000, then rand() % 3 WILL NOT generate a uniform distribution of numbers!

To understand why think of squashing the values of 0-20000 into little bins of 0, 1, or 2. There will always be leftover numbers from the range that must get dumped into a bin, so some of the bins will get filled more than others. This means that you will generate one or more of the bins more often than random, and other bins less often (depending upon the squashing operation you are using).

General rule: if you want to generate random numbers in any range DO NOT USE rand(), instead use the random library from c++11 as they give access to distributions of random numbers, which is what you want.

bobmack
Автор

if you wanted to get a number between 0-100, what arithmetic should you do to the modulo part? Thanks!

TheMrBlueKoolaid
Автор

Gracias, me acabas de salvar un esta cuarentena

LandmarkPeak
Автор

Hi! How am I able to do this and use cin as well? I've been trying to use cin along with rand but it somehow wont work

andrewy
Автор

Hello, how long did it take you to learn this language? Thank you!

eshamblazer
Автор

What im use is..f#include <iostream>
#include <random>
#include <ctime>

Int main(){
default_random_engine VARIABLE (time(0/null);
Uniform_int_distribution<int> VARIABLE(min number, max number);}


Im always use that to generate a random number..

Janszzz
Автор

sorry i have a question, why some people prefer linux to program, or make source code? but i've seen also some people that uses mac to program, like you for example. You're using an apple operating system to program..

adolfofragoso