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
Solved

How do I use random integer generators?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

837 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