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

  • Home
  • SEARCH
  • 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 1062987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:42:56+00:00 2026-05-16T18:42:56+00:00

Hiya, my code currently has three functions each producing a massive array of random

  • 0

Hiya, my code currently has three functions each producing a massive array of random numbers. Im wondering if there is a way of just having one function returning a linked list or multidimensional array to make it a bit neater:

(copied from http://pastebin.com/Y5aE6XKS)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef RAND_MAX
#define RAND_MAX 2147483648
#endif
#define N 420000

double* rdm_X(void);
double* rdm_Y(void);
double* rdm_Z(void);

void main(void)
{
   double* Random_number_list_X = rdm_X();
   double* Random_number_list_Y = rdm_Y();
   double* Random_number_list_Z = rdm_Z();
   double X[N+1], Y[N+1], Z[N+1], density = 1, vol = 42.0;
   double sum = 0, sum_x = 0, sum_y = 0, sum_z = 0;
   int i;

   for (i = 0; i <= N; i++) {
      X[i] = 3 * Random_number_list_X[i] + 1;
      Y[i] = 7 * Random_number_list_Y[i] - 3;
      Z[i] = 2 * Random_number_list_Z[i] - 1;
      if ((Z[i]*Z[i]) + (sqrt(X[i]*X[i] + Y[i]*Y[i]) - 3)*(sqrt(X[i]*X[i] + Y[i]*Y[i]) - 3) <= 1) {
         sum += density;
         sum_x += X[i] * density;
         sum_y += Y[i] * density;
         sum_z += Z[i] * density;
      }
   }
   printf("(%.5lf, %.5lf, %.5lf)\n",
            sum_x/sum, sum_y/sum, sum_z/sum);
}

double* rdm_X(void)
{
   double* Random_number_list_X = calloc(N + 1, sizeof(double));
   int i;

   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list_X[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list_X;
}

double* rdm_Y(void)
{
   double* Random_number_list_Y = calloc(N + 1, sizeof(double));
   int i;
   sleep(1);
   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list_Y[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list_Y;
}

double* rdm_Z(void)
{
   double* Random_number_list_Z = calloc(N + 1, sizeof(double));
   int i;
   sleep(2);
   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list_Z[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list_Z;
}
  • 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-05-16T18:42:56+00:00Added an answer on May 16, 2026 at 6:42 pm

    I’m not the first to point out that you should only call srand once, but I’ll explain why:

    The more often you call srand the less random the output of rand is.

    The rand function is a pseudo-random number generator. That means that it generates numbers that look random, and have mathematical properties corresponding to randomness, but they aren’t actually random. The output of rand is actually a fixed, completely deterministic sequence of numbers.

    Or, rather, it produces one of a large family of completely deterministic sequences. You select which of these sequences you want by providing a “seed” value using srand. When you give srand a seed x, the next output of rand will be the first number of the pseudo-random (but completely deterministic!) sequence identified by the seed x. In other words:

    int foo(int x)
    {
       srand(x);
       return rand();
    }
    

    Will return different values for different inputs, but for a given x, will always return the same value. Not at all random!

    This is actually a useful feature, since if you discover a bug in a program that relies on the output of rand, you can reliably reproduce the bug by providing the same seed to srand in order to obtain the same sequence from rand and so the same behavior from your program.

    The reason that you need to call srand once is because otherwise your program will always receive the same sequence of numbers from rand (the sequence identified by seed 1). The reason you do not want to call srand more than once (in most cases) is because you are then repeatedly forcing rand back to the beginnings of its sequences rather than letting it give you one of them in its entirety. While any given sequence has properties of randomness, the sequence of sequence-beginnings does not necessarily have this property.

    Obviously, it’s especially bad if you call srand repeatedly with the same seed, because then you’re forcing rand back to the beginning of the same sequence every time, and so rand will always produce the same value — exactly what you don’t want.

    The reason you commonly see srand(time(NULL)) is because the time is likely to be different between any two invocations of a given program, which means that every time the program runs it will use a different pseudorandom sequence. But time only returns the time to the granularity of seconds, so if you do this repeatedly within a single invocation of a program, as in yours, and less than one second elapses between calls to srand, you will be repeatedly re-seeding with the same seed, with ridiculous results, as you have observed.

    Bottom Line: Call srand exactly once, before your first use of rand. Trust that the implementers of the C library wrote a decent pseudo-random number generator, and don’t try to “increase randomness” by attempting to compensate for problems that don’t exist.

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

Sidebar

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.