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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:22:24+00:00 2026-06-01T13:22:24+00:00

This code has generated a 1 result for r on the 1st toss 200

  • 0

This code has generated a 1 result for r on the 1st toss 200 times in a row; the later tosses aren’t static; they change; any idea what I screwed up?? Using Xcode. Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define tprop .5    // propability coin will be tails

int main(int argc, const char *argv[])
{
    int i;          // for loop counter
    int n;          // # of rolls
    int hc = 0;     // heads counter
    int tc = 0;     // tails counter
    int r;          // roll... 1=heads, 0=tails

    srand(time(NULL));  // seed Random# generator with current time

    printf("Enter # of times to toss a coin: ");
    scanf("%i", &n);

    for (i = 0; i < n; i++) {
        if ((float) rand() / RAND_MAX > tprop) {
            r = 1;
            hc++;
        } else {
            r = 0;
            tc++;
        }
        printf("%in", r);
    }
    printf("# of times heads came up: %i (%f%%)n", hc, (float) hc / n * 100);
    printf("# of times tails came up: %i (%f%%)n", tc, (float) tc / n * 100);

    return 0;
}
  • 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-01T13:22:25+00:00Added an answer on June 1, 2026 at 1:22 pm

    To answer this question, first we need to look at rand() code.

    From Rand Implementation:

    void __cdecl srand (unsigned int seed)
    {
        #ifdef _MT
            _getptd()->_holdrand = (unsigned long)seed;
        #else /* _MT */
            holdrand = (long)seed;
        #endif /* _MT */
    }
    
    int __cdecl rand (void)
    {
       #ifdef _MT
        _ptiddata ptd = _getptd();
        return( ((ptd->_holdrand = ptd->_holdrand * 214013L + 2531011L) >> 16) &
        0x7fff );
       #else /* _MT */
        return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
       #endif /* _MT */
    }
    

    As you can see, the random value is computed based on the seed value (and a new seed value is the computed), thats why we call those numbers pseudo-random. Since we are intersted in the first run, lets simplify the code a bit, lets write a function which takes the input the seed, and return the first rand():

    int firstRand(unsigned int seed) {
        return (((seed * 214013L + 2531011L) >> 16) & 0x7fff);
    }
    

    And now lets run a test with with this function:

    for (i = 0; i < 50; ++i) {
        printf("seed = %u; rand = %d\n", seed + i, firstRand(seed + i));
    }
    

    Here I got this result:

    seed = 1333783311; rand = 19779
    seed = 1333783312; rand = 19783
    seed = 1333783313; rand = 19786
    seed = 1333783314; rand = 19789
    seed = 1333783315; rand = 19792
    seed = 1333783316; rand = 19796
    seed = 1333783317; rand = 19799
    seed = 1333783318; rand = 19802
    seed = 1333783319; rand = 19805
    seed = 1333783320; rand = 19809
    seed = 1333783321; rand = 19812
    seed = 1333783322; rand = 19815
    seed = 1333783323; rand = 19819
    seed = 1333783324; rand = 19822
    seed = 1333783325; rand = 19825
    seed = 1333783326; rand = 19828
    seed = 1333783327; rand = 19832
    seed = 1333783328; rand = 19835
    seed = 1333783329; rand = 19838
    seed = 1333783330; rand = 19841
    seed = 1333783331; rand = 19845
    seed = 1333783332; rand = 19848
    seed = 1333783333; rand = 19851
    seed = 1333783334; rand = 19854
    seed = 1333783335; rand = 19858
    seed = 1333783336; rand = 19861
    seed = 1333783337; rand = 19864
    seed = 1333783338; rand = 19868
    seed = 1333783339; rand = 19871
    seed = 1333783340; rand = 19874
    seed = 1333783341; rand = 19877
    seed = 1333783342; rand = 19881
    seed = 1333783343; rand = 19884
    seed = 1333783344; rand = 19887
    seed = 1333783345; rand = 19890
    seed = 1333783346; rand = 19894
    seed = 1333783347; rand = 19897
    seed = 1333783348; rand = 19900
    seed = 1333783349; rand = 19903
    seed = 1333783350; rand = 19907
    seed = 1333783351; rand = 19910
    seed = 1333783352; rand = 19913
    seed = 1333783353; rand = 19917
    seed = 1333783354; rand = 19920
    seed = 1333783355; rand = 19923
    seed = 1333783356; rand = 19926
    seed = 1333783357; rand = 19930
    seed = 1333783358; rand = 19933
    seed = 1333783359; rand = 19936
    seed = 1333783360; rand = 19939
    

    So, as you can see, if the seed are close, the values will probably be close to, and since you used the current time, the 200 tests you ran were all with seed values close to each other.

    time(NULL) return the current time in seconds. To get better results you should use the time in milliseconds (and if you really need the values to change a lot between 2 runs do some operation over it).

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

Sidebar

Related Questions

This code has an interesting bug: some_struct struct_array1[10] = {0}; some_struct struct_array2[10] = {0}
So this code has the off-by-one error: void foo (const char * str) {
I translated this code(it has bad side effect that it just capture the outer
I have this code that has one button that let's me choose an entry
bCrypt's javadoc has this code for how to encrypt a password: String pw_hash =
I was checking out this question which has this code - (NSArray *) percentagesRGBArray:(float[])
I'm using a tutorial plugin - http://particlebits.com/code/jquery-tutorial/ which has this code attached to the
In my application I use the ShinyBlue.xaml resource dictionary which has this code for
I'm now using ListItem() for Json format result, but the generated Json text has
In my page body, I need to insert this code as the result of

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.