3 Replies - 14283 Views - Last Post: 01 November 2005 - 03:29 PM Rate Topic: -----

#1 gryphin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 138
  • Joined: 29-October 05

Use of rand() function

Posted 29 October 2005 - 08:30 PM

I am trying to create a dice rolling program that uses the random number generating capabilities in order to have the program roll 3 6-sided dice and add them up for 6 characteristics in a RPG. I have it running to where it generates the numbers for the six characteristics, and each time the program is run it generates new numbers, but the numbers are the same for all 6 characteristics. Here is the code that I have written with the help of one of my Professors in College, but I just cannot figure out what to do to change it so it makes the six attributes different numbers (a couple repeats because of the probabilities is alright, but not all 6 the exact same...)


/*
Brian Casey
CS1400 - Lab 6
Dice Roller V. 1.0
*/

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;


// function prototypes
int setAttribute(string attribute);
int dice();


int main()
{

// this seeds the random time
srand(time(0));

// these are the individual character stats
int strength=setAttribute("str");
int dexterity=setAttribute("dex");
int constitution=setAttribute("con");
int intellegence=setAttribute("int");
int wisdom=setAttribute("wis");
int charisma=setAttribute("cha");


return 0;
}

int setAttribute(string attribute)
{

// this is the calculation to add the three dice together
int rollem=dice()+dice()+dice();

// this commands prints the attributes and its number
cout << "Your " << attribute << " is. " << rollem << "\n";
return rollem;
}

int dice()
{

// to output the random number
return rand()%6+1;
}



P.S. There is a time limit, I need help A.S.A.P. Thank you all for your time.


Gryphin

Is This A Good Question/Topic? 0
  • +

Replies To: Use of rand() function

#2 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: Use of rand() function

Posted 29 October 2005 - 11:49 PM

Someone correct me if I'm wrong, but I think you have to reseed each time, so you'd want to call srand(time(0)); each time you call rand(). Basically, srand "fakes" a random number by providing a starting point for the random number.

For instance, srand(1000); would always return the same random number, thus using srand(time(0)); return a different number each time, but because you don't reseed EACH time you call the rand() function, you get the same 6 numbers.

Here is a more detailed explanation of how srand() works: http://www.fredosaur...isc/random.html

The only thing you may run in to is srand(time(0)); works off the number of seconds in the system clock, but your program is going to run so fast you may end up with the same srand for each number.

Try that for starters, I'll let one of our experts give you a better answer if that doesn't work.
Was This Post Helpful? 0
  • +
  • -

#3 amos   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 17-April 05

Re: Use of rand() function

Posted 30 October 2005 - 04:38 AM

Below is a screenshot of what I got when I compiled and ran your code. Isn't that what it's supposed to do?

Quote

For instance, srand(1000); would always return the same random number, thus using srand(time(0)); return a different number each time, but because you don't reseed EACH time you call the rand() function, you get the same 6 numbers.


If I remember right this is wrong, the seed gives the rand() function its starting point then rand() returns a different number each subsequent time it is called. In fact if you put srand(time(0)) into the dice() function it will give you the same number each time you call dice (I think its because the time(0) function returns the same value in milliseconds because the program runs so fast).

Basically you only need to call srand once to seed the initial value of rand().

Cheers
Amos

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 gryphin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 138
  • Joined: 29-October 05

Re: Use of rand() function

Posted 01 November 2005 - 03:29 PM

Thanks to both of your replies I was able to fix the problem and get it running. Thanks....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1