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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:22:49+00:00 2026-05-21T20:22:49+00:00

I’ve been ripping my hair out for a while now with these random numbers

  • 0

I’ve been ripping my hair out for a while now with these random numbers in C++.

In Python, I had the awesome:

random.uniform(0, 1)

Which churned out a new random number each time I called it.

C++ has to have something like this. I Googled for a long time, and found erand48(), which I plan to implement into my raytracer (I’m translating it from Python to C++).

I tried a simple test case, but I was hoping to create a random_uniform() function which always spits out a new random number (using time() isn’t going to work AFAICT, as this will be running really quickly)

unsigned short Xi[3] = {0, 1, 27};
std::cout << erand48(Xi);

And the output was (and will be every time I call the program):

0.174529

I tried using the previous output as the new Xi, like this (Xis initial value was defined):

float random_uniform() {
  long generated = erand48(Xi);
  int temp = generated * 1000000;

  unsigned short Xi[3] = {temp - 16, temp - 7, temp - 18};

  return generated;
}

But that doesn’t seem like it would generate random enough numbers (and it only spits out 0. I’, not sure why…).

Is there any way that I could make a function which spits out a new random number each time?

  • 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-21T20:22:50+00:00Added an answer on May 21, 2026 at 8:22 pm

    Not being familiar with python, I’m going to assume that random.uniform(0, 1) spits out a random number from a uniform distribution between 0 and 1?

    If so, try the following:

    srand(time(NULL));
    float myRand = ((float) rand( )) / RAND_MAX;
    

    Note that your mileage may vary though – rand() is not guaranteed to be a very high quality random number source. You may get visual artifacts depending on the implementation and how you’re using it. I use a Mersenne Twister random number generator in my raytracer.

    EDIT: Just to be a little clearer, that’s:

    ...
    void init_program ( )
    {
        ...
        srand(time(NULL));
        ...
    }
    ...
    float random_uniform ( )
    {
        return ((float) rand( )) / RAND_MAX;
    }
    ...
    

    If you call both srand and rand at the same time, you’re likely to get the same “random” number each time, because unless the time has changed in the meantime you’ll seed the generator with exactly the same seed, and hence get exactly the same first random number.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.