0

Hi,
I have googled but found nothing about this.

I am trying to generate a random number from a given array. But how do i do it?

Suppose:
I have an array int arr[]={3,89,99,4,55};
and i want to generate a random number among these numbers.

thanks in advance.

2
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by nishad_forums
1

All you need to do is use rand to get a random number in the range of your array and then use that number for the index of your array. Something like this:

cout << arr[(rand() % sizeOfArray)];
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.