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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:07:57+00:00 2026-05-15T04:07:57+00:00

I have in C++ an array of 100 elements, so v[1], … ,v[100] contains

  • 0

I have in C++ an array of 100 elements, so v[1], … ,v[100] contains numbers. How can i display, 25 random numbers from this array? So i wanna select 25 random positions from this array and display the values.. How can i do this in C++?

Thanks!

    #include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <vector>

using namespace std;
int aleator(int n) 
{
    return (rand()%n)+1;
}
int main() 
{
    int r;
    int indexes[100]={0};
  //  const int size=100;
    //int a[size];
    std::vector<int>v;
    srand(time(0)); 
    for (int i=0;i<25;i++) 
    {
     int index = aleator(100);
     if (indexes[index] != 0)      
    {
        // try again
         i--;
         continue;
     }
    indexes[index] = 1;
    cout << v[index] ;
    } 
    cout<<" "<<endl;
    system("pause");
    return 0;
}

The idea is that i have this code, and i generate 100 random numbers. What i want is an array with random 25 elements from those 100 generated.. But i don’t know how to do that

Regards

  • 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-15T04:07:58+00:00Added an answer on May 15, 2026 at 4:07 am

    I modified the version of Mehdi a little in order to make it choose differnet indexes
    NOTE: This makes the algorithm not deterministic – it relies on the RNG.

    int indexes[100]={0};
    
    srand ( time (0) );
    
    for (int i=0;i<25;i++)
    {
         int index = rand() % 100;
         if (indexes[index] != 0)
         {
             // try again
             i--;
             continue;
         }
         indexes[index] = 1;
         cout << v[index] ; cout << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array with let's say, 500 elements. I know I can select
I have a form which can populate from 1-100 elements based on a user's
If I have an array with say 100 elements.. how can I echo/show the
I have an int array for which i have allocated space for 100 elements.
I have an array with integers of values from 0 to 100. I wish
In PHP I have an array that looks like this: $array[0]['width'] = '100'; $array[0]['height']
I have an array like this: int a[100]; I am filling only the first
In an C# program I have an array with about 100.000 elements. Then I
how can i have jquery switch between the array below on an elements font
I have to make an array of 100 numbers and then shuffle the first

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.