Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Random Number

Random Number

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++lounge
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P pix_programmer

    Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #3

    Since (from rand() as cplusplus.com[^]):

    Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

    An alternative is:

    int n = 1 + 6.0 * rand() / (RAND_MAX + 1.0);

    Veni, vidi, vici.

    I 1 Reply Last reply
    0
    • C CPallini

      Since (from rand() as cplusplus.com[^]):

      Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

      An alternative is:

      int n = 1 + 6.0 * rand() / (RAND_MAX + 1.0);

      Veni, vidi, vici.

      I Offline
      I Offline
      Ingo
      wrote on last edited by
      #4

      CPallini wrote:

      Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

      That's wrong. I startet a small programm generating one billion random number for each of the two solutions repeatly. The are no difference and the reason is obvious. Of course smaller numbers are more likely, but it doesn't matter how big a number is when you use modulo as it depends on divisibility. And there is no difference for small or large numbers.

      ------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.

      C 1 Reply Last reply
      0
      • P pix_programmer

        Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #5

        This should do it.

        int randomizedValueInRange = (rand()%(maxRange-minRange+1))+minRange;

        You talk about Being HUMAN. I have it in my name AnsHUMAN

        1 Reply Last reply
        0
        • I Ingo

          CPallini wrote:

          Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

          That's wrong. I startet a small programm generating one billion random number for each of the two solutions repeatly. The are no difference and the reason is obvious. Of course smaller numbers are more likely, but it doesn't matter how big a number is when you use modulo as it depends on divisibility. And there is no difference for small or large numbers.

          ------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #6

          It depends on the goodness of the pseudo random generator , of course (hint: it is not the reminder function that is 'broken'). The test depends also on the periodicity of the random generator. How did you perform the test?

          Veni, vidi, vici.

          I 1 Reply Last reply
          0
          • C CPallini

            It depends on the goodness of the pseudo random generator , of course (hint: it is not the reminder function that is 'broken'). The test depends also on the periodicity of the random generator. How did you perform the test?

            Veni, vidi, vici.

            I Offline
            I Offline
            Ingo
            wrote on last edited by
            #7

            CPallini wrote:

            How did you perform the test?

            I had just two arrays of long integer, filled with random numbers. Then I calculated the difference between the two, printed them and repeated the test, adding the differences. Of course there are differences but they are neglectable and there is no scheme that they will raise with higher numbers.

            ------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.

            1 Reply Last reply
            0
            • P pix_programmer

              Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?

              S Offline
              S Offline
              Software_Developer
              wrote on last edited by
              #8

              The code is as follows : int random_number = (rand() % 6) + 1; Example of use:

              #include <cstdlib>
              #include <ctime>
              #include <iostream>
              using namespace std ;

              int main(void)
              { //seed
              srand(static_cast<unsigned>(time(0)));

              cout<<" \\nDICE GAME\\n";
              
              //Generate a random number between 1 and 6
              int random\_number = (rand() % 6) + 1;
              
              cout<<" \\nYou threw a \["<<random\_number <<"\]\\n\\n";
              

              return 0;
              }

              1 Reply Last reply
              0
              • P pix_programmer

                Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?

                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #9

                Typically these requests for code result in a lot of sarcastic responses, and being down voted into oblivion what makes one different?

                D 1 Reply Last reply
                0
                • J jeron1

                  Typically these requests for code result in a lot of sarcastic responses, and being down voted into oblivion what makes one different?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #10

                  Where did you see a sarcastic reply in this thread?

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  C 1 Reply Last reply
                  0
                  • D David Crow

                    Where did you see a sarcastic reply in this thread?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #11

                    I suppose he is wondering why there are not.

                    Veni, vidi, vici.

                    J 1 Reply Last reply
                    0
                    • C CPallini

                      I suppose he is wondering why there are not.

                      Veni, vidi, vici.

                      J Offline
                      J Offline
                      jeron1
                      wrote on last edited by
                      #12

                      I didn't, that's why I'm curious, no 'google it' posts, no links to 'how to ask a question' specifically the part about not doing others' homework, etc... So I asked what makes this post unique?

                      C 1 Reply Last reply
                      0
                      • J jeron1

                        I didn't, that's why I'm curious, no 'google it' posts, no links to 'how to ask a question' specifically the part about not doing others' homework, etc... So I asked what makes this post unique?

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #13

                        Good mood? I don't know, really.

                        Veni, vidi, vici.

                        J 1 Reply Last reply
                        0
                        • C CPallini

                          Good mood? I don't know, really.

                          Veni, vidi, vici.

                          J Offline
                          J Offline
                          jeron1
                          wrote on last edited by
                          #14

                          Wellness Wednesday? :laugh:

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups