Jump to content
  • Advertisement
Sign in to follow this  
Lenox

Randomly Picking from Arrays?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 time

int 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Lenox
I'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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Lenox
I'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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!