C random numbers 🎲
Вставка
- Опубліковано 11 вер 2024
- 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
", number1);
printf("%d
", number2);
printf("%d
", number3);
return 0;
}
#include
#include
#include
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
", number1);
printf("%d
", number2);
printf("%d
", number3);
return 0;
}
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
I was searching for it a lot😂😂😂😂
I loved you tutorial❤ thanks
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
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😮
Thank you so much for correct libraries. Just thanks!
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.
Its so you most likely get a completely different sequence of random numbers every time you run your program, as time(NULL) or time(0) returns the amount of seconds passed since januari 1 1970, thus it always changes given enough time.
Thanks a lot! You've got a new subscriber!
can someone explain about the srand and the time library?
You make my day 🎉❤
Thanks you! You really did make my life easier :3
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?
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 🙏😭
No, he just happened to get 6 6 5 as an output. If you if you tried this, you would get different numbers (eg. 8 3 5). Taking out the random seed just makes the output the exact same set of digits every time, which is why he kept getting 6 6 5 over and over again. Hope this helped :)
@@JasonKazz thanks for the explanation ❤
Cómo se utiliza el Lrand para generar números aleatorios long?
Thanks Bro!
wow useful
Thanks a lot !!
Thank u ☺️
Bro you’re the best
whatever anything I do or did or anything
very correct. thank you
@@yigittuncer6935 😂😂
Wow usaful
int number1 = (rand() % 7);.......................Is this right ?
If you add 1 in the equation it will addd to the printed result (rand() %7) +1;
Example:
0 + 1 = 1
1 + 1 = 2
2 +1 = 3
6 + 1 = 7
If you do not want 0 for a result, like if you're trying to code a dice than you shoul add the "+1" since "0" isn't a outcome for a die to roll
for me it says that srand has an implicit declaration
Whenever you see such message after you compile your source code , it means you didn't include the header file which include the function def. Just add #include
to your source file and it should work fine
@@nullzero9224 thanks man
...and well... ya... that's how to do it!
So if I want to make a random number table thats 10x10
How do I do it without needing to write number1 - number 100?
for loop probably
Array and for loop scenario right there