LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-08-2010, 02:29 PM   #1
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Rep: Reputation: 1
rand issues with C


Put simply, the following code compiles, but does not work, and I have no clue why (despite reading extensively about rand()). In the interest of full disclosure, this is for a school project.

Code:
/*
 *  Dice.c
 *  CMSC 245, Professor [REDACTED].  Project 5.
 *
 *  [REDACTED] <[REDACTED]>
 *
 *  This program simulates rolling 100,000 die, prints the number of times a 2,
 *  7 and 12 were rolled and prints the average of all the rolls.
 */
#include <stdio.h>
#include <stdlib.h>

void printHeading();
int rollDie();

int main() {

  int i, j, seed, temp;
  const int dice = 2;
  /* The array store will always hold 6*(# of Dice) - (# of Dice) + 1 numbers.
   */
  int store[(6*dice)-(dice-1)];

  printf("Enter a seed:\t");
  scanf("%d", &seed);

  srand( seed );

  for(i = 0; i <= 100000; i++) {
    temp = rollDie() + rollDie();
    store[temp-2]++;
  }

  for(j = 0; j < 11; j++) {
    printf("%d ", store[j]);
  }

}

int rollDie() {

  /* It is best to discard any values outside the range you want so that the 
   * probabilities for each number coming up are not affected, but that takes
   * far too long, so we use the modulus operator.
   */
  return (1 + (rand() % 6));

}
For example, I had the following (very clearly impossible) set returned for a seed of 1:
Code:
2761, 5546, 8388, 11209, 1987762146, 49553, 662080544, 43823, 8439, 5462, 4199134,
The values 1987762146 (i.e. number of 6's) and 662080544 (number of 8's) change a lot with the same seed, while 43823 (number of 9's) changes slightly (which I still isn't supposed to happen?). The last value (number of 12's) doesn't budge, but is impossible to obtain with my constraints.

Anyway, I'd really appreciate any help as to why this isn't working. Thanks in advance.
 
Old 11-08-2010, 02:52 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The main problem is you didn't initialize the contents of store[]
 
1 members found this post helpful.
Old 11-08-2010, 02:56 PM   #3
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Original Poster
Rep: Reputation: 1
Well, that sure did fix it, I feel really stupid now. Thanks johnsfine!
 
Old 11-08-2010, 03:04 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by acvoight View Post
that sure did fix it
As frequently occurs, I got "interrupted" (by my real work) and pressed Submit on a partially typed reply, planning to use edit to fix it after.

I thought I needed to explain why the way you defined store[] could contain initial garbage at run time.

By the time I was ready to edit, you had solved it based on my partial reply.

I guess you knew that rule and just overlooked it while coding.
 
Old 11-08-2010, 03:14 PM   #5
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Original Poster
Rep: Reputation: 1
Yea, I completely forgot that an uninitialized array could contain garbage when I was doing it; appreciate the help
 
  


Reply

Tags
random



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
rand and array dreamlemon Programming 1 07-09-2009 06:26 PM
using rand in matlab lmvent Programming 10 02-23-2009 04:35 PM
LXer: What is Wrong with RAND? LXer Syndicated Linux News 0 04-18-2008 10:50 AM
rand() question deiussum Programming 6 11-11-2004 02:10 PM
Need help using rand() in C KneeLess Programming 12 10-01-2003 12:51 PM

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

All times are GMT -5. The time now is 01:59 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