Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6540205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:57:00+00:00 2026-05-25T10:57:00+00:00

Just switch from Python to C++ and I begin to re-write my Python tools

  • 0

Just switch from Python to C++ and I begin to re-write my Python tools in C++ for better understanding, but can’t solve this one…

This function will generate range of random numbers, for example “randomRange(12)” may return range of 12 numbers like “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’t find equivalent for question marks ” ? “

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:57:01+00:00Added an answer on May 25, 2026 at 10:57 am

    You’ll have trouble getting good randomness with high values of n, 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);
    };
    

    Here’s another approach. On 32-bit platforms, you can only do 9 digits in an int, so we’ll make the function return a double, and generate a string of ASCII digits then convert:

    #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);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be closed, a couple of minutes from now, but I think I
I've just made the switch from MS sql server to Oracle. The query tool
I'm trying to switch from two-stage rotation to one-stage rotation (to avoid the console
I am just making the switch from WebForms to MVC and would like to
Is it possible to switch from HiLo to GUID.comb? As far as I can
I'm just getting started with Python but already have found it much more productive
I just had to switch from ICEFaces dataTable to simple forEach tag, because I
I've just created a new website and am ready to switch from an my
Just found this out, so i am answering my own question :) Use a
Let me start from a real life example: Customer: Alex, just noticed something strange

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.