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 7601723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:14:01+00:00 2026-05-30T23:14:01+00:00

What is the best way to generate random numbers?

  • 0

What is the best way to generate random numbers?

  • 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-30T23:14:02+00:00Added an answer on May 30, 2026 at 11:14 pm

    If and only if:

    • you are not looking for “perfect uniformity” or

    • you have no C++11 support and not even TR1 (thus you don’t have another choice)

    then you might consider using the following C-style solution, which (for the sake of the reputation of this community ~ see rand() Considered Harmful) is written in strike-through font:

    Here’s the simple C-style function that generates random number from the interval from min to max, inclusive. Those numbers seem to be very close to being uniformly distributed.

    int irand(int min, int max) {
        return ((double)rand() / ((double)RAND_MAX + 1.0)) * (max - min + 1) + min;
    }
    

    and don’t forget to call srand before you use it:

    int occurrences[8] = {0};
    
    srand(time(0));
    for (int i = 0; i < 100000; ++i)
        ++occurrences[irand(1,7)];
    
    for (int i = 1; i <= 7; ++i)
        printf("%d ", occurrences[i]);
    

    output: 14253 14481 14210 14029 14289 14503 14235

    Also have a look at:
    Generate a random number within range?
    Generate random numbers uniformly over an entire range
    and find some time and watch at least first 11 minutes of aforementioned video

    Otherwise:

    use <random> just like it was pointed out by Kerrek SB already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to generate random numbers using Objective-C on iOS? If
What is the best non-deterministic way to generate large random numbers? I wrote a
What's the best way to generate a number X of random binary vectors of
What is the best way to generate a random DateTime in Ruby/Rails? Trying to
What is the best way to generate a random float in C#? Update: I
What's the best way to generate 5 random non duplicating integers from 0 -
What is the best way to generate a current datestamp in Java? YYYY-MM-DD:hh-mm-ss
What is the best way to generate a string of \t 's in C#?
What's the best way to generate a known number of non overlapping fixed radius
What is the best way to generate a unique hardware ID on Microsoft Windows

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.