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;
    }

КОМЕНТАРІ • 37

  • @BroCodez
    @BroCodez  3 роки тому +31

    #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;
    }

  • @user-xk7de1jw8g
    @user-xk7de1jw8g 10 місяців тому +3

    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

  • @temdemencia7274
    @temdemencia7274 3 місяці тому

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

  • @kylek3822
    @kylek3822 2 місяці тому

    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

  • @Gandingas
    @Gandingas 9 місяців тому

    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😮

  • @OBENEK
    @OBENEK 3 місяці тому

    Thank you so much for correct libraries. Just thanks!

  • @Fru1tpunch
    @Fru1tpunch Рік тому +7

    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.

    • @slonkweerd4858
      @slonkweerd4858 7 місяців тому +1

      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.

  • @evertonsston
    @evertonsston Рік тому

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

  • @nafisaswad3623
    @nafisaswad3623 10 місяців тому +2

    can someone explain about the srand and the time library?

  • @agent7gaming
    @agent7gaming Рік тому +1

    You make my day 🎉❤

  • @chapter8291
    @chapter8291 4 місяці тому

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

  • @goktugmustdie
    @goktugmustdie 24 дні тому

    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?

  • @SurajDas-zi7wb
    @SurajDas-zi7wb 2 роки тому +2

    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 🙏😭

    • @JasonKazz
      @JasonKazz 2 роки тому +3

      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 :)

    • @SurajDas-zi7wb
      @SurajDas-zi7wb 2 роки тому +1

      @@JasonKazz thanks for the explanation ❤

  • @lissettejaravaldes3620
    @lissettejaravaldes3620 Рік тому +2

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

  • @ivankevamo
    @ivankevamo 3 місяці тому

    Thanks Bro!

  • @zawnaing344
    @zawnaing344 Рік тому +2

    wow useful

  • @semanur9505
    @semanur9505 5 місяців тому

    Thanks a lot !!

  • @quitecontrary.
    @quitecontrary. 7 місяців тому

    Thank u ☺️

  • @abbatysidi3603
    @abbatysidi3603 Рік тому

    Bro you’re the best

  • @ryanalnaser914
    @ryanalnaser914 2 роки тому +4

    whatever anything I do or did or anything

  • @mohibkhan152
    @mohibkhan152 Рік тому

    Wow usaful

  • @shortsShowcase250
    @shortsShowcase250 2 роки тому +2

    int number1 = (rand() % 7);.......................Is this right ?

    • @nananakeson
      @nananakeson 2 роки тому +3

      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

    • @nananakeson
      @nananakeson 2 роки тому +3

      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

  • @dabibakare7134
    @dabibakare7134 2 роки тому +1

    for me it says that srand has an implicit declaration

    • @nullzero9224
      @nullzero9224 Рік тому +9

      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

    • @ahmetsarboga3130
      @ahmetsarboga3130 11 місяців тому

      @@nullzero9224 thanks man

  • @TheScissorunner
    @TheScissorunner Рік тому

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

  • @nokthar97
    @nokthar97 2 роки тому +1

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