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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:46:41+00:00 2026-06-14T13:46:41+00:00

I’m trying to make a game with dice, and I need to have random

  • 0

I’m trying to make a game with dice, and I need to have random numbers in it (to simulate the sides of the die. I know how to make it between 1 and 6). Using

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
    int i;
    i = (rand()%6)+1; 
    cout << i << "\n"; 
}

doesn’t work very well, because when I run the program a few times, here’s the output I get:

6
1
1
1
1
1
2
2
2
2
5
2

So I want a command that will generate a different random number each time, not the same one 5 times in a row. Is there a command that will do this?

  • 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-14T13:46:42+00:00Added an answer on June 14, 2026 at 1:46 pm

    The most fundamental problem of your test application is that you call srand once and then call rand one time and exit.

    The whole point of srand function is to initialize the sequence of pseudo-random numbers with a random seed.

    It means that if you pass the same value to srand in two different applications (with the same srand/rand implementation) then you will get exactly the same sequence of rand() values read after that in both applications.

    BUT in your example application pseudo-random sequence consists only of one element – the first element of a pseudo-random sequence generated from seed equal to current time of 1 sec precision. What do you expect to see on output then?

    Obviously when you happen to run application on the same second – you use the same seed value – thus your result is the same of course (as Martin York already mentioned in a comment to the question).

    Actually you should call srand(seed) one time and then call rand() many times and analyze that sequence – it should look random.

    AMENDMENT 1 – example code:

    OK I get it.
    Apparently verbal description is not enough (maybe language barrier or something… 🙂 ).

    Old-fashioned C code example based on the same srand()/rand()/time() functions that was used in the question:

    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    int main(void)
    {
        unsigned long j;
        srand( (unsigned)time(NULL) );
    
        for( j = 0; j < 100500; ++j )
        {
            int n;
    
            /* skip rand() readings that would make n%6 non-uniformly distributed
              (assuming rand() itself is uniformly distributed from 0 to RAND_MAX) */
            while( ( n = rand() ) > RAND_MAX - (RAND_MAX-5)%6 )
            { /* bad value retrieved so get next one */ }
    
            printf( "%d,\t%d\n", n, n % 6 + 1 );
        }
    
        return 0;
    }
    

    ^^^ THAT sequence from a single run of the program is supposed to look random.

    Please NOTE that I don’t recommend to use rand/srand functions in production code for the reasons explained below and I absolutely don’t recommend to use function time as a random seed for the reasons that IMO already should be quite obvious. Those are fine for educational purposes and to illustrate the point sometimes but for any serious use they are mostly useless.

    AMENDMENT 2 – detailed explanation:

    It is important to understand that as of now there is NO C or C++ standard features (library functions or classes) producing actually random data definitively (i.e. guaranteed by the standard to be actually random). The only standard feature that approaches this problem is std::random_device that unfortunately still does not provide guarantees of actual randomness.

    Depending on the nature of application you should first decide if you really need truly random (unpredictable) data. Notable case when you do most certainly need true randomness is information security – e.g. generating symmetric keys, asymmetric private keys, salt values, security tokens, etc.

    Actually security-grade random numbers is a separate industry worth a separate article. (I briefly touch it in this answer of mine.)

    In most cases Pseudo-Random Number Generator is sufficient – e.g. for scientific simulations or games. In some cases consistently defined pseudo-random sequence is even required – e.g. in games you may generate the same map(s) each time in runtime to save installation package size.

    The original question and reoccurring multitude of identical/similar questions (and even many misguided "answers" to them) indicate that first and foremost it is important to distinguish random numbers from pseudo-random numbers AND to understand what is pseudo-random number sequence in the first place AND to realize that pseudo-random number generators are NOT used the same way you could use true random number generators.

    Intuitively when you request random number – the result returned shouldn’t depend on previously returned values and shouldn’t depend if
    anyone requested anything before and shouldn’t depend in what moment
    and by what process and on what computer and from what generator and
    in what galaxy it was requested. That is what word "random" means
    after all – being unpredictable and independent of anything –
    otherwise it is not random anymore, right? With this intuition it is
    only natural to search the web for some magic spells to cast to get
    such random number in any possible context.

    ^^^ THAT kind of intuitive expectations IS VERY WRONG and harmful in all cases involving Pseudo-Random Number Generators – despite being reasonable for true random numbers.

    While the meaningful notion of "random number" exists (kind of) – there is no such thing as "pseudo-random number". A Pseudo-Random Number Generator actually produces pseudo-random number sequence.

    Pseudo-random sequence is in fact always deterministic (predetermined by its algorithm and initial parameters) – i.e. there is actually nothing random about it.

    When experts talk about quality of PRNG they actually talk about statistical properties of the generated sequence (and its notable sub-sequences). For example if you combine two high quality PRNGs by using them both in turns – you may produce bad resulting sequence – despite them generating good sequences each separately (those two good sequences may simply correlate to each other and thus combine badly).

    Specifically rand()/srand(s) pair of functions provide a singular per-process non-thread-safe(!) pseudo-random number sequence generated with implementation-defined algorithm. Function rand() produces values in range [0, RAND_MAX].

    Quote from C11 standard (ISO/IEC 9899:2011):

    The srand function uses the argument as a seed for a new sequence of
    pseudo-random numbers to be returned by subsequent calls to rand. If
    srand is then called with the same seed value, the sequence of
    pseudo-random numbers shall be repeated. If rand is called before any
    calls to srand have been made, the same sequence shall be generated as
    when srand is first called with a seed value of 1.

    Many people reasonably expect that rand() would produce a sequence of semi-independent uniformly distributed numbers in range 0 to RAND_MAX. Well it most certainly should (otherwise it’s useless) but unfortunately not only standard doesn’t require that – there is even explicit disclaimer that states "there is no guarantees as to the quality of the random sequence produced".
    In some historical cases rand/srand implementation was of very bad quality indeed. Even though in modern implementations it is most likely good enough – but the trust is broken and not easy to recover.
    Besides its non-thread-safe nature makes its safe usage in multi-threaded applications tricky and limited (still possible – you may just use them from one dedicated thread).

    New class template std::mersenne_twister_engine<> (and its convenience typedefs – std::mt19937/std::mt19937_64 with good template parameters combination) provides per-object pseudo-random number generator defined in C++11 standard. With the same template parameters and the same initialization parameters different objects will generate exactly the same per-object output sequence on any computer in any application built with C++11 compliant standard library. The advantage of this class is its predictably high quality output sequence and full consistency across implementations.

    Also there are other (much simpler) PRNG engines defined in C++11 standard – std::linear_congruential_engine<> (historically used as fair quality srand/rand algorithm in some C standard library implementations) and std::subtract_with_carry_engine<>. They also generate fully defined parameter-dependent per-object output sequences.

    Modern day C++11 example replacement for the obsolete C code above:

    #include <iostream>
    #include <chrono>
    #include <random>
    
    int main()
    {
        std::random_device rd;
        // seed value is designed specifically to make initialization
        // parameters of std::mt19937 (instance of std::mersenne_twister_engine<>)
        // different across executions of application
        std::mt19937::result_type seed = rd() ^ (
                (std::mt19937::result_type)
                std::chrono::duration_cast<std::chrono::seconds>(
                    std::chrono::system_clock::now().time_since_epoch()
                    ).count() +
                (std::mt19937::result_type)
                std::chrono::duration_cast<std::chrono::microseconds>(
                    std::chrono::high_resolution_clock::now().time_since_epoch()
                    ).count() );
    
        std::mt19937 gen(seed);
    
        for( unsigned long j = 0; j < 100500; ++j )
        /* ^^^Yes. Generating single pseudo-random number makes no sense
           even if you use std::mersenne_twister_engine instead of rand()
           and even when your seed quality is much better than time(NULL) */    
        {
            std::mt19937::result_type n;
            // reject readings that would make n%6 non-uniformly distributed
            while( ( n = gen() ) > std::mt19937::max() -
                                        ( std::mt19937::max() - 5 )%6 )
            { /* bad value retrieved so get next one */ }
    
            std::cout << n << '\t' << n % 6 + 1 << '\n';
        }
    
        return 0;
    }
    

    The version of previous code that uses std::uniform_int_distribution<>

    #include <iostream>
    #include <chrono>
    #include <random>
    
    int main()
    {
        std::random_device rd;
        std::mt19937::result_type seed = rd() ^ (
                (std::mt19937::result_type)
                std::chrono::duration_cast<std::chrono::seconds>(
                    std::chrono::system_clock::now().time_since_epoch()
                    ).count() +
                (std::mt19937::result_type)
                std::chrono::duration_cast<std::chrono::microseconds>(
                    std::chrono::high_resolution_clock::now().time_since_epoch()
                    ).count() );
    
        std::mt19937 gen(seed);
        std::uniform_int_distribution<unsigned> distrib(1, 6);
    
        for( unsigned long j = 0; j < 100500; ++j )
        {
            std::cout << distrib(gen) << ' ';
        }
    
        std::cout << '\n';
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an array which has BIG numbers and small numbers in it. I
I am trying to loop through a bunch of documents I have to put
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.