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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:15:19+00:00 2026-06-09T14:15:19+00:00

Possible Duplicate: How does a random number generator work? I am looking for internal

  • 0

Possible Duplicate:
How does a random number generator work?

I am looking for internal implementation of a random number generator in C/C++.Basically I am interested to know, what exactly happens when rand() is called. After all machine follows definite set of instructions, how can it be random!
Edit: Want to know how can I implement one in C/C++.

  • 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-09T14:15:21+00:00Added an answer on June 9, 2026 at 2:15 pm

    They’re pseudo-random number generators, not truly random ones. This is often a good thing since it allows you to reproduce bugs more easily where "random" numbers are involved.

    You can get random number generators, such as reading /dev/random under Linux but the normal ones that ship with C libraries generally aren’t.

    The simplest one are linear congruential generators where:

    nx+1 = (nx * A + C) modulo M

    with suitably chosen values of A, C and M.

    Wikipedia’s page on LCGs gives some sample values used by various implementations. For example, the glibc one listed there has A = 1103515245, C = 12345, M = 2^31 so it’s a simple thing like:

    static unsigned int seed = 1;
    void srand (int newseed) {
        seed = (unsigned)newseed & 0x7fffffffU;
    }
    int rand (void) {
        seed = (seed * 1103515245U + 12345U) & 0x7fffffffU;
        return (int)seed;
    }
    

    Aside: The glibc implementation still has this generator within it (called the Type 0 generator) but it also has a fancier trinomial generator as well, which is (presumably) better.

    There are also more complex ones (such as the Mersenne twister) that have a much greater cycle time (time before starting to repeat).

    Any truly random generator must use a truly random input source which is why /dev/random will sometimes block "waiting for entropy", while /dev/urandom won’t.

    "Truly" random sources may be affected by timing between keystrokes, data entered by users, the contents of network packets, disk I/O patterns, time taken for an ICMP response to come back over the network and all sorts of other wondrous, mostly non-deterministic things.

    Unless you’re heavily into crypto, normal random number generators should be just fine.

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

Sidebar

Related Questions

Possible Duplicate: Why does it appear that my random number generator isn't random in
Possible Duplicate: Why does this Random Number Generator not random? I have this test
Possible Duplicate: Why does it appear that my random number generator isn't random in
Possible Duplicates: Why does it appear that my random number generator isn't random in
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: How does this bash fork bomb work? Today one of my friend
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know
Possible Duplicate: How does github change URL without reloading a page? I don't know
Possible Duplicate: Does an open-ended interval implementation exist for Java? i have an int

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.