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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:09:36+00:00 2026-06-01T15:09:36+00:00

Possible Duplicate: Why do I always get the same sequence of random numbers with

  • 0

Possible Duplicate:
Why do I always get the same sequence of random numbers with rand()?

So yeah, this might seem slightly noobish, but since I’m teaching myself C after becoming reasonable at Java, I’ve already run into some trouble. I’m trying to use the rand() function in C, but I can only call it once, and when it do, it ALWAYS generates the same random number, which is 41. I’m using Microsoft Visual C++ 2010 Express, and I already set it up so it would compile C code, but the only thing not working is this rand() function. I’ve tried including some generally used libraries, but nothing works. Here’s the code:

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

int main(void)
{
    printf("%d", rand()); //Always prints 41

    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-01T15:09:37+00:00Added an answer on June 1, 2026 at 3:09 pm

    This because the rand() initializes its pseudo-random sequence always from the same point when the execution begins.

    You have to input a really random seed before using rand() to obtain different values. This can be done through function srand that will shift the sequence according to the seed passed .

    Try with:

    srand(clock());   /* seconds since program start */
    srand(time(NULL));   /* seconds since 1 Jan 1970 */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Python dictionary: are keys() and values() always the same order? If i
Possible Duplicate: How was the first compiler written? This question has always been bothering
Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: catch exception by pointer in C++ I always catch exceptions by value.
Possible Duplicate: Are iframes considered 'bad practice'? While working with web developers, I always
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: What’s the Right Way to use the rand() Function in C++? When
Possible Duplicate: In MySQL queries, why use join instead of where? Joins are always
Possible Duplicate: jQuery Ajax always returns “undefined”? I'm trying to come up with a

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.