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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:48:58+00:00 2026-05-26T03:48:58+00:00

int rng(int min, int max){ int result = min + ( ( (max –

  • 0
int rng(int min, int max){
    int result = min + ( ( (max - (min - 1) ) * rand() ) / (RAND_MAX + 1) );
    return result;
}

This won’t compile with ideone using c99 mode, but does so when I’m using gcc on CodeBlocks while on -std=c99. I figured using long int would do the trick, but apparently not so. I use ideone when I’m trying some stuff in school since they just have VC++ and I’m doing C C99. I wouldn’t want to not use this function when I need it, and so here I am asking for what is wrong.

  • 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-26T03:48:59+00:00Added an answer on May 26, 2026 at 3:48 am

    Actually, on my compiler this produces warning: integer overflow in expression [-Woverflow].

    The problem is caused by RAND_MAX + 1. RAND_MAX often happens to be the same as INT_MAX. Adding 1 to INT_MAX of course yields undefined behavior (except the compiler catches at compile time and replaces the code with something else).

    If you want to see in action why it’s bad, do this:

    scanf("%d", &dummy); /* So the compiler won't catch it. */
    dummy += INT_MAX;
    
    gcc -ftrapv -Wall -Wextra # ftrapv is the key point
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int x = n / 3; // <-- make this faster // for instance
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
Here is my method: public static string GenerateRandomString(int bytes) { var rng = new
If I create salt by using something like this: public class User { private
I'm using the RNG crypto provider to generate numbers in a range the truly
I'm having trouble with Javas Random class, if i do this: Random rng =
I'm trying to compile this example But that one header isn't enough and I've
Right now I'm using the Mersenne Twister RNG and performing Fisher-Yates shuffle algorithm 100
int ii, maxnum; for(ii=1; ii<=num-1; ii++) { if(count[ii]>max) { // the part where I
int main() { double i=4; printf(%d,i); return 0; } Can anybody tell me why

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.