473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

programming help, random number generation

I need help on getting started with a dice program that will output as many random numbers from 1 to 6 and as many rolls as the user requests, and then how many times each number shows up from the number of rolls (occurrences)...and the corresponding percent. I'm supposed to use a loop (while, for) and some "do". . . I'm lost. Please help!

So far, this is what I have. . .
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int main ()
  7. {
  8.   int x;
  9.   int Rolls;
  10.   int DiceNumber;
  11.   int Number;
  12.   int FinalRoll;
  13.   int D1;
  14.   int D2;
  15.   int D3;
  16.   int D4;
  17.   int D5;
  18.   int D6;
  19.   double Occurances;
  20.   double Percent;
  21.  
  22. //Initialize Random Seed
  23.   srand ( time(NULL) );
  24.  
  25. //Rolling die
  26.   Number = rand() %6 + 1;
  27.  
  28.     x = 0
  29.     D1 = 0
  30.     D2 = 0
  31.     D3 = 0
  32.     D4 = 0
  33.     D5 = 0
  34.     D6 = 0
  35.  
  36.   do {
  37.       printf("Enter the number of times you want to roll the die:\n");
  38.       scanf("%d", &Rolls);
  39.       if
  40.       while (x < Rolls) {
  41.  
  42.     Percent = Occurances / Rolls
  43.     printf("%6c,%3d,%6c%3d%8c%7.2f\n", DiceNumber, ' ', Occurances, ' ', Percent);
  44.  
  45.   return 0;
  46. }
Feb 19 '08 #1
1 1512
RRick
463 Expert 256MB
The best way to deal with a program like this is one issue at a time.

As for your do-while loop, its all messed up. My suggestion is ignore the "do" part and replace it with a single while loop. It will look something like while( xxx) { ... }, where xxx is the conditional (you got that right) and ... is the code.

You got Number right, but you only generate a single one. You want to generate "Rolls" number of them, so you need to put that inside the while loop.

You need to store the values of Number somewhere. I suspect that what D1 ... D6 are for. If you want to use them, you will have to create a big if-else strucure that checks each possible value for Number and then up dates the correct variable. You could instead use an array (with 7 not 6 items) and just update each cell in the array by using Number as an index to the array. Both ways work, but one uses a lot less code.

Finally, you need to print out the results. This is done after the while loop.
Feb 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
12
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
3
by: TD | last post by:
I found a freeware dll, md5lib.dll, on the web and am trying to use it in Acess 2003. I entered this in a module in the database: Public Declare Function StringMD5 Lib...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.