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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:15:34+00:00 2026-06-15T12:15:34+00:00

I have a simple program to generate N pairs of random numbers in C.

  • 0

I have a simple program to generate N pairs of random numbers in C.

I am using rand() for this. The value of N is a command line parameter.

I noticed that it always generates the same stream of digits when the passed command line arg is the same.

The rand() manpage says that if there is no explicit seed, the RNG is implicitly seeded by value 1 (there no call to srand() in my program).

Then however, when I call my program with arg=10 and then with arg=12, I should see the first 10 digits of both sequences as equal, right? (They will be both using the same seed, namely 1). This is not happening. It looks like the seed is being derived and applied from the arg passed implicitly, but that doesn’t seem likely. Any ideas?

Update: [Sorry for not posting this earlier]

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])  
{  
    int objs=100, i;  
    if (argc >= 2)  
        objs = atoi(argv[1]);

     for(i=0; i<objs; i++){
         int p = rand() % objs;
         int q = rand() % objs;
         printf("%d %d\n", p, q);
      }
      return 0;
}

I am using Ubuntu 12 and gcc 4.7.2.

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

    The int version of rand will generate random numbers from 0 to RAND_MAX. Those ints will be the same with the same seed. But, there must be another step in your code which converts that number to one in the range 0-10 or 0-12. That scaling will be responsible for the different of output.

    For example, if it uses division, and rand returns 25, then:

    // for range 0..10,
    25 / 11 => 2 (truncated)
    
    // for range 0..12,
    25 / 13 => 1 (truncated)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a simple shell script or c program to generate random files
I have a simple program on windows using visual studios 2008. In my code
I have this simple program that computes salaries for four different worker types. It's
Making a simple program which will generate a multiple choice form. I have an
I have a command-line C program (DOS/Windows) which produces a hex dump of several
In my program I want to draw a simple score line graph. I have
Say I have simple program that emulates a board game with a number of
I have Simple java program named MainController.java. Wehn I try to compile it from
I have a simple program. I want to set the path and some properties
I have a simple program that checks webpages for strings, example: Private Sub Button1_Click(ByVal

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.