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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:38:42+00:00 2026-06-07T12:38:42+00:00

I am using rand() to generate either 0 or 1 ( rand() % 2

  • 0

I am using rand() to generate either 0 or 1 (rand() % 2). I am seeding it using the current time (srand(time(NULL))).

After much debugging, I realised that rand() never returns an even (odd) number 16 or more times in a row.

Is this a known issue? Is there a better PRNG that comes with C?

I am running on Windows 7 using Visual Studio 2010.

  • 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-07T12:38:44+00:00Added an answer on June 7, 2026 at 12:38 pm

    Instead of using rand()%2, try rand()>(RAND_MAX/2). You can only assume rand() to be uniform on the interval [0, RAND_MAX].

    Edit: This was suggested by Shahbaz in the comments, which I only noticed after I posted this answer.

    Edit: ArjunShankar called me out on my previous wording: “rand() is only specified to be uniform on the interval [0, RAND_MAX]”

    From the C99 standard:

    The rand function computes a sequence of pseudo-random integers in the range 0 to
    RAND_MAX.

    Technically, uniformity (or equidistributed) is not specified, but is the de-facto standard used for implementations of commonly used PRNG’s (e.g. Mersenne Twister). This is to allow a programmer to easily create a custom PRNG with a non-uniform distribution. Without this property, a programmer is forced to implement a custom PRNG from scratch.

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

Sidebar

Related Questions

I've used #include<stdlib> #include<time> using namespace std; srand((unsigned)time(0)); int n=(rand()>>8)%4; but what other random
I'm using SRand/Rand to generate an array of random numbers. The array size depends
I'm trying to generate a bunch of random numbers using rand() % (range). Here's
I want to get randomized results for a query. I read that using RAND()
I've got an unmanaged c++ console application in which I'm using srand() and rand().
If I generate a number using: $num=rand(0, 1000); , is it possible to guess
I understand, using srand(time(0)), helps in setting the random seed. However, the following code,
Possible Duplicate: Creating Nondeterministic functions in SQL Server using RAND() I need to generate
Possible Duplicate: Generating random numbers in C using rand to generate a random numbers
i'm trying to generate a unique id using this code: $RCode = md5(uniqid(rand(), true));

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.