LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2013, 11:42 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Random numbers aren't the size I want them


I'm using the fuction below and I can't figure out when I used dice(6); why it produces numbers larger then 6. The function below that one is the function I am using and the results are under the code.

Code:
short int dice(int size)
{
     short int rc;

     rc = (rand() % (size - 1 + 1) + 1);
     return rc;
}

bool dice_attributes(struct players *ch)
{
     short int roll[4] = { 0, 0, 0, 0};

     roll[0] = dice(6);
     roll[1] = dice(6);
     roll[2] = dice(6);
     roll[3] = dice(6);
     ch->str += (roll[0] + roll[1] + roll[2] + roll[3]);
     roll[0] = dice(6);
     roll[1] = dice(6);
     roll[2] = dice(6);
     roll[3] = dice(6);
     ch->dex += (roll[0] + roll[1] + roll[2] + roll[3]);
     roll[0] = dice(6);
     roll[1] = dice(6);
     roll[2] = dice(6);
     roll[3] = dice(6);
     ch->con += (roll[0] + roll[1] + roll[2] + roll[3]);
     roll[0] = dice(6);
     roll[1] = dice(6);
     roll[2] = dice(6);
     roll[3] = dice(6);
     ch->in  += (roll[0] + roll[1] + roll[2] + roll[3]);
     roll[0] = dice(6);
     roll[1] = dice(6);
     roll[2] = dice(6);
     roll[3] = dice(6);
     ch->wis += (roll[0] + roll[1] + roll[2] + roll[3]);
     roll[0] = dice(6);
     roll[1] = dice(6);
     roll[2] = dice(6);
     roll[3] = dice(6);
     ch->cha += (roll[0] + roll[1] + roll[2] + roll[3]);
}
Strength : 25
Dexterity : 24
Charisma : 18
Intelligence : 25
Wisdom : 28
Constitution : 30
 
Old 09-20-2013, 11:51 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,782

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You should use = instead of +=. Or else set the stats of ch to 0 before calling dice_attributes().

Also, the - 1 + 1 in dice is redundant.
 
1 members found this post helpful.
Old 09-20-2013, 11:55 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Your size mod expression is probably not what you intend (size -1 +1), maybe a typo?

The value in ch is not necessarily zero, so explicitly set it to zero first, or better yet, use = instead of += in those lines.

Dang! ntubski was faster on the submit!

Last edited by astrogeek; 09-20-2013 at 11:56 PM.
 
1 members found this post helpful.
Old 09-20-2013, 11:56 PM   #4
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Then I realize I was testing my back input and I kept adding things to my stats because of the += thanks very much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed - replacing random numbers followed with space and more random chars gluposti Linux - Newbie 7 05-07-2012 07:36 PM
Random numbers in C loke137 Programming 9 09-11-2010 10:37 AM
Random numbers between -5.0 to +5.0 smp Linux - Newbie 12 08-17-2010 06:57 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration