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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:21:28+00:00 2026-06-13T00:21:28+00:00

I have some issues with C++ implementation of a function that generates random integers

  • 0

I have some issues with C++ implementation of a function that generates
random integers with fixed distribution. To do this I have implemented
a function urand() that generates a double uniformly distributed between 0 and 1 (included)
used by the function random_integer() [which uses a standard algorithm, I think it is described in one of Knuth’s bool].
The functions are implemented like this:

double urand(){
int r =rand(); ;
return ((double) r)/RAND_MAX ;
} ; // uniform random number between 0 and 1 (included);


int random_integer(std::vector<double> &p_vec) {
unsigned int n_events=p_vec.size();
double u;
double initu;
do{
    u=urand();
}while(u<=0 && u >=1);
// chose a random number uniformly distributed between 0 and 1 (excluded)
initu=u;
for (unsigned int i=0; i<n_events; i++){
    if (u<=p_vec[i]) return i;
    u-=p_vec[i];
}
cout << "Warning: get_random_event() [utilities.cpp] could not find a suitable event. Returning -1 instead.\n";
cout << "p_vec=[" ; print(p_vec); cout << "]; "; // print is just a function that displays the elements in a vector;
cout << "initu=" << initu << "; u=" << u << endl;
return -1 ;
 }

Now most of the time everything works fine, but for example I got a warning like this:

Warning: get_random_event() [utilities.cpp] could not find a suitable event.
Returning -1 instead.
p_vec=[0.08;0.42;0.42;0.08[; initu=1; u=2.77556e-17

Now there are two things I don’t understand: initu should be strictly less than 1 (look at the condition for the while loop). But even assuming that it i

  • 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-06-13T00:21:29+00:00Added an answer on June 13, 2026 at 12:21 am

    I think you meant

    }while(u<=0 || u >=1);
    

    not

    }while(u<=0 && u >=1);
    

    u can’t ever be both less or equal to than zero and greater than or equal to one.

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

Sidebar

Related Questions

I have some issues with a PHP file that is not working properly. The
I have a function that converts from my own implementation of a 3D vector
I have some issues in setting the classpath for the a project for which
I have some issues with my EJB 3 timer where it is launched more
I'm have some issues adding a class to 2 navigation links on a theme.
I seems to have some issues in order to compile gproc from uwiger. I
Im starting with CoCos2d development and I think I have some issues with nodespace,
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
I am integrating PayPal in my website But I have some issues in it.
I ran git pull origin and now I have some issues with merge my

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.