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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:22:33+00:00 2026-05-30T08:22:33+00:00

Was working on a project with mingw on windows : C:\Users\…>g++ -dumpversion 4.5.0 When

  • 0

Was working on a project with mingw on windows :

C:\Users\...>g++ -dumpversion
4.5.0

When I compiled the code under g++ v 4.2.4 I was getting a segmentation fault – after couple hours I pinned it down to the line :

double decimal = ((double) rand()) / (RAND_MAX + 1);

For some reason this was giving negative values (and one thing led to another).
What reason ?

Edit : cpp :

#include <iostream>
#include "Random.h"
#include <math.h>

using namespace std;
double Random::exponential(int T) {
    double decimal = ((double) rand()) / (RAND_MAX + 1);
    //  std::cout << "decimal : " << decimal << std::endl;
    return log(1 - decimal)*(-T);
}
//etc

h:

#ifndef RANDOM_H
#define RANDOM_H

#include <cstdlib>
#include <math.h>
class Random {
public:
    static double exponential(int T);
    static int random_int(int min, int max);
    static bool coin(); //50% true 50% false
};

#endif  /* RANDOM_H */

just noticed the double include (of math.h) but this shouldn’t be an issue

  • 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-30T08:22:34+00:00Added an answer on May 30, 2026 at 8:22 am

    In your case, RAND_MAX is the maximum value for the integer type it is stored in, so RAND_MAX + 1 gives you the maximum negative value. Technically this is signed integer overflow which is undefined behaviour so anything can happen.

    You need to do, as J-16 pointed out,

    double decimal = (double)rand() / ((double)RAND_MAX + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for a small game project with SDL on MinGW/Windows. I am working
I had a working project. After rearranging some code, I tried to recompile my
I'm working on project that lets users choose some scientific authors and columnists and
I'm working on converting a Linux project of mine to compile on Windows using
I compiled an fine working project (before compiled with 3.1.3) now with 3.2.1 Now
Has anyone had luck getting Qt Eclipse integration working on windows? Here is what
I'm working on a C project with Eclipse and MinGW. When running a debug
I have been working on getting a C++ IDE going on my 64-bit Windows
While working a project tonight, I ended up using one .js resource file for
I working on project and have problem with threading and update of UI. I

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.