tags:

views:

101

answers:

3

Hello,

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

+3  A: 

Short Answer
Use std::random_shuffle(v.begin(),v.end()) to shuffle the array, and then display the first 25 elements.

Long Answer
First of all, the elements would be v[0]...v[99] (C++ uses 0-based indexing), not v[1]...v[100]. To answer your question, though, it depends on whether it is acceptable to repeat elements of the array or not. If you aren't worried about repeats, then simply use the index rand()%v.size(), repeatedly until you have selected a sufficient number of indices (25 in your question). If repeats are not acceptable, then you need to shuffle the array (by swapping elements at random), and then display the first (or last, or any contiguous region of) N elements (in this case N=25). You can use std::random_shuffle to shuffle the array. That does the bulk of the work for you. Once you've done that, just show 25 elements.

Michael Aaron Safyan
And how should i show only 25 elements, randomly selected? :)
qwerty
Edited the first post, with my code
qwerty
use iterators (v.begin(), v.begin()+25)
jk
+1  A: 

If you want to print 25 numbers of an array V you can use this code do:

int V[100]={1,2,5,...} ;

srand ( time (0) ) ;

for (int i=0;i<25;i++)

{

cout << V[rand() % 100 + 1]<<" " ;

}
Mehdi Mahmoudi
Yes, random 25 numbers (positions) of an array, but using the code from the 1st post, so 25 random numbers within those generated..:)
qwerty
A: 

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;
}
Iulian Şerbănoiu
Iulian, related to my first post, what should i change? I have there my code. I need an array, and cout << v[rand() % n]? Because i habe there that aleator() function ..Thanks
qwerty
So in my case, instead of int index = rand() % 100; i should have int index = aleator(100) +1 ? and i cout<< then v[index] ? Mersi:)
qwerty
Take those random numbers and add 1, and of course adapt your code. My program works from 0 to 99 (which is normal in C where everything is 0 based). Please try to be "0 based" - you'll eliminate a lot of problems, trust me. Good luck !
Iulian Şerbănoiu
Iulian, i edited the 1st post so now i have the code using arrays. However, it compiles successfully but at runtime.. "crapa". Din ce cauza? What i'm doing wrong? I got that error with 'Send error report' and 'Don't send'
qwerty