?
Solved

Random Number Generation from a specified set

Posted on 2003-02-20
6
Medium Priority
?
174 Views
Last Modified: 2010-04-15
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);
0
Comment
Question by:skips2ndbase
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 7991333
#define setsize 6 // u know ur set size;
int set[]={1,4,7,10,13,16};

srand(time(NULL));

int gimmeRandomFromSet(int *set,int setsize){
int j;
j=(int) (((float)setsize)*(rand()/(RAND_MAX+1.0)));
// 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,setsize);
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 7991337
int randfromset=gimmeRandomFromSet(set,setsize);

now use randfromset
0
 
LVL 8

Expert Comment

by:Exceter
ID: 7991388
#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;
}

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:Exceter
ID: 7991394
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()%ARRAYSIZE]);
   return 0;
}

Exceter
0
 
LVL 8

Accepted Solution

by:
Exceter earned 150 total points
ID: 7991399
>> #incldue <time.h>

#include <time.h>
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 7994144
hmm i guess my solution was good enough.. whatever followed was just a rewrite..
anyways not for the first time with me
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question