1 Replies - 2205 Views - Last Post: 13 February 2012 - 03:04 AM

#1 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Roll a Die

Posted 15 March 2009 - 08:40 PM

Description: You need:
#include<iostream>
and:
using namespace std;This function simulates a roll of a die and returns a random number between one and 6
//******************************************************
//This function simulates a roll of a die and returns  
//a random number between one and 6
//Author: OliveOyl                    
//******************************************************

int roll()
{
    int roll = rand() % 6 + 1;
    return roll;
}

//EXAMPLE USAGE:
int main()
{
    //random seed
    srand (time(NULL));
    
    int roll1, roll2, roll3;
    
    //call function for each of three rolls
    roll1 = roll();
    cout<<"Your first roll is a: " <<roll1 <<endl;

    roll2 = roll();
    cout<<"Your second roll is a: " <<roll2 << endl;

    roll3 = roll();
    cout<<"Your third roll is a: "<<roll3 <<endl;

getchar();
return 0;
}



Is This A Good Question/Topic? 1
  • +

Replies To: Roll a Die

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Roll a Die

Posted 13 February 2012 - 03:04 AM

Note that the dice roll if implemented this way has a small bias, read http://www.dreaminco...1&#entry1545152
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1