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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:19:57+00:00 2026-05-14T00:19:57+00:00

I am taking a course on programming, and we’re using C++. We had an

  • 0

I am taking a course on programming, and we’re using C++.
We had an assignment where, at some point, we needed to code a function that would return a random number in an [upper, lower] interval. I used the following:

lower + (int) (upper * (rand() / (RAND_MAX + 1.0)));

I did not forget to change srand by using srand((unsigned int) time(0)).

However, I get the same value every time! I asked my professor for help and he, after some investigation, found out that the first number generated by rand() isn’t that random… The higher order bits remained unchanged, and since this implementation uses them, the end result isn’t quite what I expected.

Is there a more elegant, yet simple solution than to discard the first value or use remainders to achieve what I want?

Thanks a lot for your attention!

~Francisco

EDIT: Thank you all for your input. I had no idea rand() was such a sucky RNG 😛

  • 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-14T00:19:57+00:00Added an answer on May 14, 2026 at 12:19 am

    Given that rand() is not a very strong random number generator, the small amount of bias added by the standard approach is probably not an issue: (higher-lower) needs to be smaller than MAX_RAND of course.

    lower + rand() % (higher-lower+1);
    

    fixed off by one error.

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

Sidebar

Related Questions

I'm currently taking a course in Java and I've run into some confusing code.
I am taking an intermediate programming course which stresses the use of invariants. I
I am only allowed to use private members in the programming course I'm taking,
I'm taking a programming languages course and we're talking about the extern C declaration.
I'm taking a course in computational complexity and have so far had an impression
I am taking a software engineering course and we use Java to implement some
I have started taking the course Programming methodology(CS106A) available at stanford website. But I
I am taking a principles of programming languages course right now but I cannot
I remember when I was in some course of C programming, a teacher once
I'm taking a course on programming languages and the answer to when is a

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.