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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:01:11+00:00 2026-06-14T07:01:11+00:00

Possible Duplicate: Same random numbers every loop iteration Here is my code: #include <iostream>

  • 0

Possible Duplicate:
Same random numbers every loop iteration

Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int attack() {
    srand((unsigned)time(NULL));
    int hits=0;
    for (int i=0;i<=3;i++) {
        int a = rand() % 6;
        if (a==6 || a==5)
        {
         hits++;   
        }
    }
    return hits;
}

int main () {
    int a=attack();
    cout << a << endl;
    int b=attack();
        cout << b << endl;
    int c=attack();
        cout << c << endl;
    int d=attack();
        cout << d << endl;
    int e=attack();
        cout << e << endl;
    int f=attack();
        cout << f << endl;
    int g=attack();
        cout << g << endl;
    int h=attack();
        cout << h << endl;
}

why are all my variables in main() the same number?

I only get outputs like this:

1
1
1
1
1
1
1
1
  • 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-14T07:01:12+00:00Added an answer on June 14, 2026 at 7:01 am

    srand() should be called once at the start of the program (well, at some point before you start calling rand() anyway).

    By calling it every time you enter the function (in quick succession), you’re resetting the seed value to the same thing, which is why you’re seeing duplicate numbers.

    Note that you may occasionally get a different number if the time clicks over to a new second while your program is running, but it’s not that likely since that particular program should finish in far less than one second.

    Move the srand() call to main() before the first call to attack().

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

Sidebar

Related Questions

Possible Duplicate: Why do I always get the same sequence of random numbers with
Possible Duplicate: System.Random keeps on returning the same value I'm refactoring and expanding a
Possible Duplicate: jQuery: more than one handler for same event I am using a
Possible Duplicate: Java random always returns the same number when I set the seed?
Possible Duplicate: Java random always returns the same number when I set the seed?
Possible Duplicate: Random variable not changing in “for” loop in windows batch file I
Possible Duplicate: Generating random numbers effectively I need to generate random numbers in my
Possible Duplicate: F# getting a list of random numbers I'm getting random value from
Possible Duplicate: Using same MySQL Connection in different PHP pages This is completely new
Possible Duplicate: Are “div > p” & “div p” same? Here's the page i'm

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.