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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:18:47+00:00 2026-05-26T05:18:47+00:00

I was toying around with arrays, populating with pseudo random numbers, finding minimum and

  • 0

I was toying around with arrays, populating with pseudo random numbers, finding minimum and maximum values and their indices and number of occurrences and I noticed something strange – when using srand seeded with time the number of minimum and maximum value occurrences is ALWAYS equal. This doesn’t seem very random to me.

Is there an alternative way of getting a DIFFERENT number of minimum and maximum value occurrences, as one would expect with random numbers?

Here is my code (I am learning so it may be messy and inefficient, recommendations are welcome)

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

void findExtremes( const int[], int); 

int main()
{
    const int lenght = 2000; //define lenght

    int array1[lenght];

    srand(time(0)); 

    for ( int i = 0; i < lenght; i++) //populate array with random numbers and print them out
    {
        array1[i] = rand() % 3000;
        cout << "Index " << i << " = " << array1[i] << endl;
    }

    findExtremes(array1, lenght);   // call fn

    return 0;
}

void findExtremes( const int array[], int size)
{
     int maxV, minV, maxI, minI;
     maxV = array[0];
     minV = array[0];
     minI = 0;
     maxI = 0;

     for ( int i = 1; i < size; i++)
     {
         if ( array[i] > maxV)
            {
                maxV = array[i];
                maxI = i;
            }
         if ( array[i] < minV)
            {
                minV = array[i];
                minI = i;
            }
     }

     //find the number of occurances for min and max values

     int minOcc = 0;
     int maxOcc = 0;

     for ( int i = 1; i < size; i++)
     {
             if (array[i] == minV)
                 minOcc++;
             if (array[i] == minV)
                 maxOcc++;
     }

     //output

     cout << "\nMinmim value is index " << minI << " with value " << minV << " and " << minOcc << " occurances" << endl;
     cout << "\nMaxium value is index " << maxI << " with value " << maxV << " and " << maxOcc << " occurances" << endl << "\n";  
}
  • 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-26T05:18:48+00:00Added an answer on May 26, 2026 at 5:18 am

    For a start, they’re actually pseudo random numbers, not random numbers. In any case, it may be that a a truly random sequence has that exact property that you’re seeing 🙂 The sequence 1,1,1,1,1 is as equally likely to occur in a truly random set as much as 5,2,4,2,99.

    If you want a “more random” random sequence, I wouldn’t be using the normal ones shipped with C libraries (unless those libraries were written by people who understand randomness) – you should look into things like the Mersenne Twister, using /dev/random (if under Linux) and so on.

    You may also want to look at this snippet of your code.

    if (array[i] == minV)
        minOcc++;
    if (array[i] == minV)
        maxOcc++;
    

    I believe that last if should be comparing with maxV rather than minV. Otherwise there is zero chance that your minimum and maximum counts will be different.

    When I make that change (and change % 3000 to % 30, to get a range of duplicates), I see:

    Minmim value is index 112 with value 0 and 65 occurances
    Maxium value is index 24 with value 29 and 58 occurances
    

    And, not that it really matters in terms of this question, you may want to clean up your spelling somewhat:

    • lenght -> length.
    • minmum -> minimum
    • maxium -> maximum
    • occurances -> occurrences
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently toying around with the Clarity .NET Facebook API but am finding certain
I've been toying around with switching from ms-access files to SQLite files for my
I'm toying around with GWT (dunno if this matters) and Hibernate. I've created a
So, I'm toying around with Stackless Python and a question popped up in my
I have a scenario that I haven't been able to solve: I'm toying around
I am relatively new to matchers. I am toying around with hamcrest in combination
i am haveing some problems with JSON and arrays. I have been messing around
I am learning C and I am playing around with pointers and arrays. I
I was toying around with some of the linq samples that come with LINQPad.
I have been Googling and toying around with different methods and I am still

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.