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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:59:40+00:00 2026-06-09T00:59:40+00:00

I have to run a simulation where each event E_i has a certain probability

  • 0

I have to run a simulation where each event E_i has a certain probability P_i to happen (i=1..4, sum_i P_i = 1).

I would like to write a function that, taken as input the four different probabilities (they depend on other things happening in my simulation), returns the integer i for the chosen event.

Which is the best way to write such a function (possibly only using standard libraries,
I have to run it on a cluster where I don’t own the permissions to add new libraries)?.

I was thinking to write something like:

int get_event(double p1,double p2, double p3, double p4){
    double r=((double) rand())/(RAND_MAX); // alternatively here there can be any function that
    // generates a random number uniformly distribute in (0,1);
    if (r<=p1) return 1;
    else if (r>p1 && r <=(p1+p2)) return 2;
    else if (r>(p1+p2) && r <=(p1+p2+p3)) return 3;
    else if (r>(p1+p2+p3) && r <=1) return 4;
    else return -1; // -1 is an en error code;
}

but I am not sure if this is the best approach. Any suggestion?
Thanks a lot in advance.

  • 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-09T00:59:41+00:00Added an answer on June 9, 2026 at 12:59 am

    That’s pretty good, I’d suggest only a few small changes:

    int get_event(double p1,double p2, double p3, double p4){
        double r=((double) rand())/(RAND_MAX);
        if (r<=p1) return 1;
        r-= p1;
        if (r<=p2) return 2;
        r-= p2;
        if (r<=p3) return 3;
        r-= p3;
        if (r<=p4) return 4;
        return -1; // -1 is an en error code;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of stored procs which I would like to all run
FYI: I have edited this significantly since my first edition. This simulation has been
I have a TabControl that contains several tabs. Each tab has one UserControl on
In a monte-carlo simulation I store a summary of each run in a data
I've run simulations which have given me data points corresponding to X number of
I have run into a situation where I want to ensure that a compound
I don't know if you have run into a similar situation but here's a
I seem to run into this situation quite a lot and have yet to
I have a strange situation in regards to xml serialization... If I run MyApp.exe
I have run into a problem with Eclipse Indigo, attempting to start up 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.