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

  • Home
  • SEARCH
  • 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 7011029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:04:30+00:00 2026-05-27T22:04:30+00:00

I would like to generate uniform random numbers in C++ between 0 and 1,

  • 0

I would like to generate uniform random numbers in C++ between 0 and 1, in a way which does not use the standard rand() and srand(time(NULL)) method. The reason for this is that if I run the application more than once within the same second of my clock, the seed will be exactly the same and produce the same output.

I do not want to rely on boost or OS/compiler specifics. x86 can be assumed.

It seems as though an alternate way to do this is to use TR1 (I do not have C++11) and seeding with /dev/random in some way?

Right now I have this, but it still uses time(NULL) as a seed which will not work well within 1 second runs:

#include <iostream> 
#include <tr1/random> 

int main() 
{ 
  std::tr1::mt19937 eng; 
  eng.seed(time(NULL)); 
  std::tr1::uniform_int<int> unif(1, RAND_MAX); 
  int u = unif(eng); 
  std::cout << (float)u/RAND_MAX << std::endl; 
} 
  • 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-27T22:04:31+00:00Added an answer on May 27, 2026 at 10:04 pm

    Posting at request of the OP:

    This is still somewhat compiler-specific, but will still work on nearly all x86-targeting compilers:

    #ifdef _WIN32
    
    //  Windows
    #define rdtsc  __rdtsc
    
    #else
    
    //  For everything else
    unsigned long long rdtsc(){
        unsigned int lo,hi;
        __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
        return ((unsigned long long)hi << 32) | lo;
    }
    
    #endif
    
    int main() 
    { 
      std::tr1::mt19937 eng; 
      eng.seed( rdtsc() );    //  Seed with rdtsc.
      std::tr1::uniform_int<int> unif(1, RAND_MAX); 
      int u = unif(eng); 
      std::cout << (float)u/RAND_MAX << std::endl; 
    } 
    

    The idea here is to seed your random number generator with the rdtsc cycle-counter.

    The reason why this works is because the rdtsc cycle-counter iterates at about (often the same) speed as the CPU frequency. Therefore, the chances of two calls to it returning the same value are extremely slim – thereby, making it an excellent seed for a RNG.

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

Sidebar

Related Questions

I would like to generate infinite stream of numbers with Rand monad from System.Random.MWC.Monad
I would like to generate a random floating point number between 2 values. What
For example I would like to generate 5 unique numbers between 1 & 10.
I would like to generate random points on a 3D box defined by its
I have written this random password script which works perfectly. Although I would like
I would like to generate POST request to a server which requires authentication. I
I would like to generate Perl Expect code automatically, does something like autoexpect exist
i would like to generate dynamic tabs. so anchor tags will not have id
I would like to generate some pseudorandom numbers and up until now I've been
I would like to generate a random text using letter frequencies from a book

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.