Solved

How do I use random integer generators?

Posted on 1998-10-30
3
160 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now