C++ How I can get random value from 1 to 12? [dupl

2019-02-19 09:15发布

问题:

This question already has an answer here:

  • Generating random integer from a range 12 answers

How I can get in C++ random value from 1 to 12?

So I will have 3, or 6, or 11?

回答1:

Use the following formula:

M + rand() / (RAND_MAX / (N - M + 1) + 1), M = 1, N = 12

and read up on this FAQ.

Edit: Most answers on this question do not take into account the fact that poor PRN generators (typically offered with the library function rand()) are not very random in the low order bits. Hence:

rand() % 12 + 1

is not good enough.



回答2:

#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <time.h>

// initialize random seed
srand( time(NULL) );

// generate random number
int randomNumber = rand() % 12 + 1;

// output, as you seem to wan a '0'
cout << setfill ('0') << setw (2) << randomNumber;

to adress dirkgently's issue maybe something like that would be better?

// generate random number
int randomNumber = rand()>>4; // get rid of the first 4 bits

// get the value
randomNumer = randomNumer % 12 + 1;

edit after mre and dirkgently's comments



回答3:

Is there some significance to the leading zero in this case? Do you intend for it to be octal, so the 12 is really 10 (in base 10)?

Getting a random number within a specified range is fairly straightforward:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
 */

    int divisor = RAND_MAX/(limit+1);
    int retval;

    do { 
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}

(The while loop is to prevent skewed results -- some outputs happening more often than others). Skewed results are almost inevitable when/if you use division (or its remainder) directly.

If you want to print it out so even one-digit numbers show two digits (i.e. a leading 0), you can do something like:

std::cout << std::setw(2) << std::setprecision(2) << std::setfill('0') << number;

Edit: As to why this works, and why a while loop (or something similar) is needed, consider a really limited version of a generator that only produces numbers from, say, 0 to 9. Assume further that we want numbers in the range 0 to 2. We can basically arrange the numbers in a table:

0 1 2
3 4 5 
6 7 8 
9

Depending on our preference we could arrange the numbers in columns instead:

0 3 6  
1 4 7
2 5 8
    9

Either way, however, we end up with the one of the columns having one more number than any of the others. 10 divided by 3 will always have a remainder of 1, so no matter how we divide the numbers up, we're always going to have a remainder that makes one of the outputs more common than the others.

The basic idea of the code above is pretty simple: after getting a number and figuring where in a "table" like one above that number would land, it checks whether the number we've got is the "odd" one. If it is, another iteration of the loop is executed to obtain another number.

There are other ways this could be done. For example, you could start by computing the largest multiple of the range size that's still within the range of the random number generator, and repeatedly generate numbers until you get one smaller than that, then divide the number you receive to get it to the right range. In theory this could even be marginally more efficient (it avoids dividing the random number to get it into the right range until it gets a random number that it's going to use). In reality, the vast majority of the time, the loop will only execute one iteration anyway, so it makes very little difference what we execute inside or outside the loop.



回答4:

You can do this, for example:

#include <cstdlib>
#include <cstdio>
#include <time.h>

int main(int argc, char **argv)
{
    srand(time(0));

    printf("Random number between 1 and 12: %d", (rand() % 12) + 1);
}

The srand function will seed the random number generator with the current time (in seconds) - that's the way it's usually done, but there are also more secure solutions.

rand() % 12 will give you a number between 0 and 11 (% is the modulus operator), so we add 1 here to get to your desired range of 1 to 12.

In order to print 01 02 03 and so on instead of 1 2 3, you can format the output:

printf("Random number between 01 and 12: %02d", (rand() % 12) + 1);


回答5:

(rand() % 12 + 1).ToString("D2")



标签: c++ random