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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:14:17+00:00 2026-05-27T03:14:17+00:00

I am trying to build a random array of length (size1). The way that

  • 0

I am trying to build a random array of length (size1). The way that I’ve researched doing this is to have two separate arrays, one for my random numbers and a secondary “checking” array to make sure the numbers don’t repeat. These are labeled (shuffle) and (visit) in my code respectively. count1 is an integer for counting through my for loop.

I have included the following in different combinations and it hasn’t worked.

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

The code I seem to be struggling with is this:

    srand((unsigned)time(0));

for (count1 = 0; count1 < size1; count1++)
{
    num = (rand()%size1);
        if (visit[num] == 0)
        {
            visit[num] = 1;
            shuffle[count1] = num;
        }
}
  • 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-27T03:14:17+00:00Added an answer on May 27, 2026 at 3:14 am

    It is easier to fill your array with the numbers 0 to size1-1 and then shuffle those numbers.

    So in c like code (haven’t used c in a long time):

    for (int count = 0; count < size1; count++) {
        shuffle[count] = count;
    }
    

    and then shuffle it

    for (int index = 0; index < size1; index++) {
        int shuffleIndex = index + rand() % (size1 - index);
        int temp = shuffle[shuffleIndex];
        shuffle[shuffleIndex] = shuffle[index];
        shuffle[index] = temp;
    }
    

    Basically, for the first element in the array, select any index and switch the values. Now the first index is randomly selected. For the second element, select a random index (excluding the first as that has already been randomly selected) and do the same. Repeat until you get to the last element.

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

Sidebar

Related Questions

I'm trying to build a web application that generates a random number within a
I'm trying to build a markov generator that takes an arbitrary length for the
i'm trying to build a little module for a site that pulls 4 random
I'm trying to build a control logic that depends on the current gsp page
I am trying to build an array of custom object references. Here is some
I am trying to build an 2D array with the size of from 1D
we're trying to build an application that records the decibel level every seconds or
i am trying to build up a single.php page. this page is used to
I'm trying to build a bookmarklet that does the following: 1.)loads an external js,
I need to build a function that selects a random row of a field,

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.