LinuxQuestions.org
Help answer threads with 0 replies.
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-18-2013, 07:07 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Trouble with Random numbers and C


I have this function that dices a lot the function
below and it does it all really fast and all the
numbers are almost always the same. Is there a way I
can perform the bottom function without producing the
same results for each attribute.

Code:
short int dice(int size)
{
     short int rc;
     /* srand Must be different for different random numbers,
        it seeds the function rand */
     srand((17237210 * time(NULL)));
     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]);
}
 
Old 09-18-2013, 07:15 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
You should only call srand() once at the beginning of your program. Otherwise, every rand() is using the same seed (assuming your program is fast enough to perform all the rand() calls in under a second).
 
1 members found this post helpful.
Old 09-18-2013, 07:18 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Wow that works so much better, thank you.
 
  


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 05:05 AM.

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