Public Group

# Randomly Picking from Arrays?

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I'm making a program in C++, and I need some help (if possible). I'm at a point, where I add stuff to an array, and then I need to pick random stuff from that array that I just added stuff to. I'm curious about how i'd do this with Visual C++? I was thinking of looping through each "entry" in the array, then somehow just picking ONE random entry in that array, and adding it to the list, any help would be appreciated..Thank you.

##### Share on other sites
arrray[rand()%array_size];

##### Share on other sites
You could also do a random shuffle of the array if you don't need to preserve it's initial order:
#include <iostream>    // For cout and endl#include <algorithm>   // For random_shuffle#include <cstdlib>     // For srand#include <ctime>       // For timeint main() {  srand ( static_cast<unsigned int>(time(NULL)) );  int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };  std::random_shuffle ( &array[0], &array[9] );  for ( int i = 0; i < 10; ++i )    std::cout << array << std::endl;  return 0;}

- Neophyte

##### Share on other sites
well, if I did the random shuffle of the array that Neophyte suggested, but then i'd have to reorder it, which I'd like to avoid, so i'm going for graveyard filla's idea. Thank you for the help, ya'll.

##### Share on other sites
I'm thinking that you should also be aware that there is anothere difference between the two suggested methods: the first one can produce repetitions, the second one (randomly reordering) will not if you just iterate through it.

Hope it helps.

##### Share on other sites
Well, I've decided to go with the first one, the array[rand()%array_size] mostly because it's a dice "System" I am making, and as we all know, dice aren't ALWAYS completely random, so it's fine. I'm just having one problem, how to determine the size of an array? (array[rand()%array_size]) . I can't use strlen, since it's for strings (obviously). Any help would be appreciated, Thank You.

##### Share on other sites
Quote:
 Original post by LenoxI'm just having one problem, how to determine the size of an array?
It can't be done. Use std::vector (and look in <algorithm>; you'll find quite a few useful things in there pertaining to random distribution).

##### Share on other sites
As Oluseyi said, you can use Vector instead. That's not neccesary though.
You should know in advance how big your array is. You're the one who allocated it, after all. How big did you make it? Just save that value. :)

##### Share on other sites
Actually, the space to be allocated will be different every time. It really depends on what the user inputs for Sides and Rolls. Well, I should go allocate space for it, seeing as how I really forgot to do that first. X_X.

##### Share on other sites
Quote:
Original post by Oluseyi
Quote:
 Original post by LenoxI'm just having one problem, how to determine the size of an array?
It can't be done. Use std::vector (and look in <algorithm>; you'll find quite a few useful things in there pertaining to random distribution).

Bah.. Are you completely certain about that? IIRC, there was an example of how to do this in a book I have.. Unfortunately, I don't have the vook with me now, as it's 4 in the morning, and it's not here with me, but I'll see to posting the coe later on if I can find it.

Lenox: You say that a person enters a number for sides, well, just save that number as well as creating an array from it, and you're all set! At least for that problem [smile]

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002290
×