C random numbers 🎲

preview_player
Показать описание
C random number generator tutorial example explained

#C #random #numbers

int main()
{
//pseudo random numbers = A set of values or elements that is statistically random
// (Don't use these for any sort of cryptographic security)

// Use current time as a seed for random # generator
srand(time(0));

// rand() generates a pseudo random # between 0 - 32,767
int number1 = (rand() % 6) + 1;
int number2 = (rand() % 6) + 1;
int number3 = (rand() % 6) + 1;

printf("%d\n", number1);
printf("%d\n", number2);
printf("%d\n", number3);

return 0;
}
Рекомендации по теме
Комментарии
Автор

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
//pseudo random numbers = A set of values or elements that is statistically random
// (Don't use these for any sort of cryptographic security)

// Use current time as a seed for random # generator
srand(time(0));

// rand() generates a pseudo random # between 0 - 32, 767
int number1 = (rand() % 6) + 1;
int number2 = (rand() % 6) + 1;
int number3 = (rand() % 6) + 1;

printf("%d\n", number1);
printf("%d\n", number2);
printf("%d\n", number3);

return 0;
}

BroCodez
Автор

I love that way you teach anything. Besides your teaching strategy, your voice and the way you speak captured my imagination. Once I listen something you say, I never forget it. Keep up your good work. Thank you for your great tutorial

HunaBopa
Автор

If rand() returned a random number between 0-6 and you took mod 6 your possible values for each roll would be 0, 1, 2, 3, 4, 5, 0. Which will bias your results. Even a very small amount of bias will show up in some applications (an can be exacerbated by code that assumes a uniform distribution on [0..N-1] where N is your mod (more so if N is say 1/3 of RAND_MAX). furthermore, rand() may be implemented poorly which usually results in (shockingly) bad randomness for low bits in other words at low mods ).
There is also seed bias which in practice further limits your use cases to tasks were then number of states is less than your seed (unsigned int for srand).

Also rand returns a number 0-RAND_MAX inclusive, not 32767, RAND_MAX can and often is 32767, but this should be checked. I believe glibc uses 2^31-1

kylek
Автор

This is good thanks, I wish I knew how to come up with such a clever solutions on my own without having to look at what someone else did, props to the people who made those libraries😮

Gandingas
Автор

Thank you so much for correct libraries. Just thanks!

OBENEK
Автор

I was searching for it a lot😂😂😂😂
I loved you tutorial❤ thanks

temdemencia
Автор

Thanks man, I didn’t know about the time part of the function (which was tripping me up)

marshmelliott
Автор

why do you use the time library to seed random numbers? I saw my professor in a lecture and I still have no idea why except he used null inside the time function.

Frutpunch
Автор

can someone explain about the srand and the time library?

nafisaswad
Автор

Thanks a lot! You've got a new subscriber!

evertonsston
Автор

Thanks you! You really did make my life easier :3

chapter
Автор

When you checked out the random seed, why did it print 6 6 5 ?
It should print 6 6 6 right ?

Help me understand please 🙏😭

SurajDas-ziwb
Автор

Cómo se utiliza el Lrand para generar números aleatorios long?

lissettejaravaldes
Автор

so i set the current time as a seed so whenever i run the program it will always roll the dice for me, else it gets stuck in one roll. did i get it correct?

goktugmustdie
Автор

whatever anything I do or did or anything

ryanalnaser
Автор

...and well... ya... that's how to do it!

TheScissorunner
Автор

for me it says that srand has an implicit declaration

dabibakare
Автор

So if I want to make a random number table thats 10x10
How do I do it without needing to write number1 - number 100?

nokthar