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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:50:46+00:00 2026-06-01T15:50:46+00:00

I have a method in a class as follows… class foo{ int bar::randomNum10to50(){ srand

  • 0

I have a method in a class as follows…

class foo{
   int bar::randomNum10to50(){
      srand (time(NULL));
      int random10to50 = rand()%50+10; 
      return random10to50;
   }
}

However when I call it from main (just to check the output, because i wasn’t getting the behaviour from the program I expected) like so….

foo create;
for (int i=0; i<20;i++){
    cout<<create.randomNum10to50()<<endl;
}

it’s exactly the same number every time it’s run (ie, 9,9,9,9,9,….; next run: 43,43,43,43,…..) I don’t know what is going wrong.
The code runs very quickly so I was thinking that MIGHT be the issue but I don’t see why there wouldn’t be a difference even slightly between the 20 iterations of it. Any thoughts are appreciated! Thanks!

  • 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-01T15:50:48+00:00Added an answer on June 1, 2026 at 3:50 pm

    You need to call srand() once, outside of the randomizer function. Otherwise, you re-seed the random number generator each time with the exact same time value, producing the same initial “random” value.

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

Sidebar

Related Questions

I have a __new__ method as follows: class MyClass(object): def __new__(cls, *args): new_args =
So I have a class with a method as follows: public class SomeClass {
I have a class with a static method modifying a static variable as follows,
I have a C# class method that return a xml document not file. How
I have a method as follows (from a class than implements TBB task interface
I have added the onError method to AppModel as follows: class AppModel extends Model
I have overridden the tableView:indentationLevelForRowAtIndexPath method in my UITableViewController derived class as follows: -
I have a method inside my gnutls .c file as follows int gnutls_error_is_fatal (int
I have a generic method in a class as follows private static Dictionary<Type, Feed>
If I have a C++ method declaration as follows: class A { public: double

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.