Equivalent to this Python random number generator in C ++?

Just switch from Python to C ++, and I will start rewriting my Python tools in C ++ for a better understanding, but can't solve this problem ...

This function will generate a range of random numbers, for example, "randomRange (12)" can return a range of 12 numbers, such as "823547896545"

Python:

  def randomRange(n):
        range_start = 10**(n-1)
        range_end = (10**n)-1
        return randint(range_start, range_end)

  number = randomRange(12)

C ++:

  int n;
  int randomRange(n){
        int range_start = ?
        int range_end = ?
        int result = ?(range_start, range_end);
        return (result);
  };

  int number = randomRange(12);

I can not find the equivalent for the question marks "?"

+5
source share
5 answers

You will have trouble getting good randomness with high n values, but:

#include <math.h>         // for pow()
#include <stdlib.h>       // for drand48()

long randomRange(int n)
{
    // our method needs start and size of the range rather 
    // than start and end.
    long range_start = pow(10,n-1);
    long range_size = pow(10,n)-range_start;
    // we expect the rand48 functions to offer more randomness
    // than the more-well-known rand() function. drand48()
    // gives you a double-precision float in 0.0-1.0, so we 
    // scale up by range_size and and to the start of the range.
    return range_start + long(drand48() * range_size);
};

. 32- 9 int, return double ASCII-, :

#include <math.h>         // for pow()
#include <stdlib.h>       // for atof()

// arbitrary limit
const int MAX_DIGITS = 24;

double randomRange(int n)
{
    char bigNumString[ MAX_DIGITS+1 ];
    if (n > MAX_DIGITS)
    {
        return 0;
    }
    // first digit is 1-9
    bigNumString[0] = "123456789"[rand()%9];
    for (int i = 1; i < n; i++)
    {
        // subsequent digits can be zero
        bigNumString[i] = "0123456789"[rand()%10];
    }
    // terminate the string
    bigNumString[i] = 0;
    // convert it to float
    return atof(bigNumString);
};
+4

boost:

http://www.boost.org/doc/libs/1_47_0/doc/html/boost_random/tutorial.html#boost_random.tutorial.generating_integers_in_a_range

++ 11 :

http://en.cppreference.com/w/cpp/numeric/random

int randomRange(n)
{
    int range_start = (int)pow(10, n-1);
    int range_end = (int)pow(10, n) - 1;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dist(range_start, range_end);
    return dist(gen);
}

, , , dist(gen), , .

+2

rand, :

int result = (rand() % (range_end - range_start)) + range_start;

, rand() . modulo lo , .

( )

, -, ( ). . .

+1

, , python, ++ , . mersenne twister, , .

0

- :

#include <ctime>
#include <cmath>
unsigned long long randomRange(unsigned int n){
    static bool first = true;
    if (first)
         srand((unsigned int)time(nullptr)); //seed the generator the first time
    //as others say, std::pow to get the range
    double range_start = std::pow(10.0, n-1.0);
    double range_end = std::pow(10.0, double(n))-1.0;
    unsigned long long range = range_end-range_start;
    //generate a random number between 0 and 2^64 (minimum)
    unsigned long long result 
        = rand()*RAND_MAX*RAND_MAX*RAND_MAX
        + rand()*RAND_MAX*RAND_MAX
        + rand()*RAND_MAX
        + rand();
    //force it into range.
    return unsigned long long(result % range + range_start);
};

unsigned long long number = randomRange(12);

( - ), rand() , modulo . , rand(), , RAND_MAX, (, , , . windows/linux/whatever) , n 1 ~ 17. ~ 17, , 64- ~ 19,26 , , .

0

All Articles