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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:32:28+00:00 2026-05-20T01:32:28+00:00

Hello dear members of stackoverflow I’ve recently started learning C++, today I wrote a

  • 0

Hello dear members of stackoverflow I’ve recently started learning C++, today I wrote a little game but my random function doesn’t work properly. When I call my random function more than once it doesn’t re-generate a number instead, it prints the same number over and over again. How can I solve this problem without using for loop?
Thanks

#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int rolld6();

int main()
{
    cout<<rolld6()<<endl;
    cout<<rolld6()<<endl;
    system("PAUSE");
    return 0;

}

int rolld6()
{
    srand(time(NULL));
    return rand() % 6 + 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-05-20T01:32:28+00:00Added an answer on May 20, 2026 at 1:32 am

    srand(time(NULL)); should usually be done once at the start of main() and never again.

    The way you have it will give you the same number every time you call rolld6 in the same second, which could be a lot of times and, in your sample, is near guaranteed since you call it twice in quick succession.

    Try this:

    #include "stdafx.h"
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    
    int rolld6 (void) {
        return rand() % 6 + 1;
    }
    
    int main (void) {
        srand (time (NULL));
        std::cout << rolld6() << std::endl;
        std::cout << rolld6() << std::endl;
        system ("PAUSE");
        return 0;
    }
    

    One other thing to keep in mind is if you run this program itself twice in quick succession. If the time hasn’t changed, you’ll get the same two numbers in both runs. That’s only usually a problem when you have a script running the program multiple times and the program itself is short lived.

    For example, if you took out your system() call and had a cmd.exe script which called it thrice, you might see something like:

    1
    5
    1
    5
    1
    5
    

    It’s not something you usually do but it should be kept in mind on the off chance that the scenario pops up.

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

Sidebar

Related Questions

Hello I am compiling a program with make but I get the error of
Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Hello Dear StackOverflowers, I am new to web programming and finding the server-client mixture
If I have two strings .. say string1=Hello Dear c'Lint and string2=Dear .. I
Hello all i got a mail code like this body = Dear & cName
Introduction Hello folks, I recently learned to program in C! (This was a huge
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello I was writing a Regular Expression (first time in my life I might
Hello can anybody solve this please I'm creating the object in the action class

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.