If i had an Array as seen below, is it possible to use the rand function to randomly select an element within that particular array; if so how can this be done. The Value of the element will be outputted.

Any help much appreciated...:)

int Array[14]={10,10,10,10,11,6,9,2,3,15,11,15,16,15};

Recommended Answers

All 12 Replies

Does this help?

int RandomInt(const int MAX)
{
	//produce an int from 0 to MAX-1
	return rand() % MAX; 
}

Dave

const int arraySize=sizeof(Array)/sizeof(*Array);
cout << Array[rand()%arraySize] << endl;

Here is something I have used.

#include <iostream>
#include <cstdlib>
#include <math.h>
#include <time.h>

using namespace std;

void seed(){
    srand(time(0));
}

double Randunif(){
    return rand() / double(RAND_MAX);
}

double Randunif(double x){
	return ((Randunif()*x)+1);
}


int main() {
	
	seed();
	r=Randunif(14); //---->14 is size of array (check if value is 14, decrement by 1 since array indexing is 0-based in C++)
	cout << floor(r); // to see which index of array will be chosen
        cout << Array[r];
	return 0;
}

Hope this helps :)

Thanks Guys.

This method works great, thanks Aranarth. Here's what i got:

srand ( time(NULL) );
 int Array[]={1,2,6,7,8,89};
		
const int arraySize=sizeof(Array)/sizeof(*Array);
cout << Array[rand()%arraySize] << endl;

Although i don't really understand the method, Perhaps you could enlighten me Aranarth.

That wasn't my solution, so I'll let Aranarth answer, but I can suggest that if you use an std::vector<int> instead of an array the syntax gets much lighter - that is yourArray.size() vs sizeof(yourArray)/sizeof(*yourArray)

That wasn't my solution, so I'll let Aranarth answer, but I can suggest that if you use an std::vector<int> instead of an array the syntax gets much lighter - that is yourArray.size() vs sizeof(yourArray)/sizeof(*yourArray)

cool, but how exactly would i list all the elements within if i were to use a Vector. For intance with an array it would be:

int Array[]={1,2,6,7,8,89};

but how would this be applied when using a Vector....:)

Yea that is one thing I never figured out why they didn't include! I think you have to do:

array.push_back(1);
array.push_back(2);
array.push_back(6);
etc

Annoying, but the flexibility you get once it is built is MUCH worth it.

Yea that is one thing I never figured out why they didn't include!

The upcoming C++ standard will remedy that, though.
With a recent GCC, you can write:

vector<int> vec({1,2,6,7,8,89});

Sweet, i see what you mean Dave

This is what ive got:

vector<int>array;
         array.push_back(1);
         array.push_back(2);
         array.push_back(6);

const int arraySize=array.size();

cout << array[rand()%arraySize] << endl;

BTW what does the .size(); function actually do here. :)

.size() tells you how many elements are in the vector.

commented: Thanks Dave really appreciate the help :) +1

Awesome, Thread Solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.