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

qwerty
  • 1,967
  • 6
  • 22
  • 22
  • 1
    Do you want your program to be able to pick twice the same element (for example, v[4], v[7], and then again v[4]) ? Or do you want to pick them at most once ? And just a slight correction : if you have an array of 100 elements, then v[0], ..., v[99] contains numbers. – Shelldon Jun 03 '10 at 08:20
  • Elements can be repeated.. so i need only 25 of them, random.. how can this be done? – qwerty Jun 03 '10 at 08:32
  • See Mehdi's and my version. I believe it does what you want. – INS Jun 03 '10 at 08:39
  • Duplicate of [Randomizing elements in an array?](http://stackoverflow.com/questions/813935/randomizing-elements-in-an-array) – JoeG Jun 03 '10 at 08:43
  • What's with the "i=1" and "i<=n" business? You do realize that C++ uses 0-based indexing, right? And you are going to overrun the array, if you go include the value n (by using "<=" instead of "<"). – Michael Aaron Safyan Jun 03 '10 at 08:53
  • C++ uses 0-based indexing. but it's wrong if i write i=1 and i<=n? – qwerty Jun 03 '10 at 08:57
  • 1
    yes it will be wrong because e[100] will be invalid. – Esben Skov Pedersen Jun 03 '10 at 10:35
  • 1
    referring to your example: initialize the vector v.resize(100) and populate it with elements (integer values). (and please use English language here, it is best for everyone) – INS Jun 03 '10 at 11:20

3 Answers3

3

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
  • 93,612
  • 16
  • 138
  • 200
1

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]<<" " ;

}
  • Yes, random 25 numbers (positions) of an array, but using the code from the 1st post, so 25 random numbers within those generated..:) – qwerty Jun 03 '10 at 08:56
0

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;
}
INS
  • 10,594
  • 7
  • 58
  • 89
  • 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 Jun 03 '10 at 08:42
  • 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 Jun 03 '10 at 08:51
  • 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 ! – INS Jun 03 '10 at 08:58
  • 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 Jun 03 '10 at 09:10