skips2ndbase
asked on
Random Number Generation from a specified set
How do I go about generating a random number from a specified set?
For instance, I need to generate a number from the set { 1, 4, 7, 10, 13, 16);
For instance, I need to generate a number from the set { 1, 4, 7, 10, 13, 16);
int randfromset=gimmeRandomFro mSet(set,s etsize);
now use randfromset
now use randfromset
#include <iostream>
#include <stdlib.h>
using namespace std;
#define ARRAYSIZE 6
void main()
{
srand(time(NULL));
int set[]={1,4,7,10,13,16};
cout << "Randomly generated " << set[rand()%size] << " from the specified set of numbers." << endl;
}
#include <stdlib.h>
using namespace std;
#define ARRAYSIZE 6
void main()
{
srand(time(NULL));
int set[]={1,4,7,10,13,16};
cout << "Randomly generated " << set[rand()%size] << " from the specified set of numbers." << endl;
}
Dang!!! I forgot that I was in the C topic area. "-)
#include <stdio.h>
#include <stdlib.h>
#incldue <time.h>
#define ARRAYSIZE 6
int main()
{
int set[]={1,4,7,10,13,16};
srand(time(NULL));
printf("Randomly generated %i from the specified set of numbers.\n",set[rand()%ARR AYSIZE]);
return 0;
}
Exceter
#include <stdio.h>
#include <stdlib.h>
#incldue <time.h>
#define ARRAYSIZE 6
int main()
{
int set[]={1,4,7,10,13,16};
srand(time(NULL));
printf("Randomly generated %i from the specified set of numbers.\n",set[rand()%ARR
return 0;
}
Exceter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hmm i guess my solution was good enough.. whatever followed was just a rewrite..
anyways not for the first time with me
anyways not for the first time with me
int set[]={1,4,7,10,13,16};
srand(time(NULL));
int gimmeRandomFromSet(int *set,int setsize){
int j;
j=(int) (((float)setsize)*(rand()/
// above gives u random number between 0 and setsize;
// so ur random number is now..set[j]
return set[j];
}
calll it like this
gimmeRandomFromSet(set,set