Solved

How do I use random integer generators?

Posted on 1998-10-30
3
167 Views
Last Modified: 2010-04-02
I'm trying to make a program that simulates dice rolling, but I don't know how to use what header files to include, or what commands I need to use.
0
Comment
Question by:Jelo
[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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
pagladasu earned 50 total points
ID: 1176623
To simulate a dice, you need random nos ranging from 1 to 6. You can try the following:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int yourfunction(){
   //obtain random no seed - using time so that different nos are generated each time
   srand( (unsigned)time( NULL ) );
   i=(rand()%6)+1;
  //i will now contain randomly generated nos from 1 to 6
  return i;
}

Hope this will help.
Thanks
Pagladasu
0
 

Author Comment

by:Jelo
ID: 1176624
Thaks for the help, but you could explain, in detail, what each part of the function does?
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1176625
The srand() function is used to generate a random number. I am providing the time() function so that the seed will be different each time.
The rand() function generates random numbers based on the seed from 0 to, I think RAND_MAX.
Obtaining the remainder (using %6), I will get an integer between 0 and 5 to which I add 1 to ensure that the value is between 1 and 6.
Thanks,
Pagladasu
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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

687 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