473,581 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

programming help, random number generation

1 New Member
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 1534
RRick
463 Recognized Expert Contributor
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
2492
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 spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone...
10
741
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 rand(): #include <stdlib.h> #include <time.h>
23
2267
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 as fast as one block can fall, and the next to be determined. Generating different numbers in one spur can work, but people can play Tetris for...
12
6330
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 0. but thats not i want to do. I want to recurse back until until it find a correct number. i will post the function which i need the help; ...
22
3420
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
13494
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
7553
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; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
16
539
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 given a seed. - Relatively uniform, does not have to be perfect. The application is not a security or statistics application, the quality of numbers...
3
4884
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 "C:\Windows\system32\md5lib.dll" Alias "MD5" (ByVal pwd As String) As String I put the md5lib.dll file in C:\Windows\system32\md5lib.dll. On a simple form to test it with...
0
7862
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8301
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5361
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3803
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1400
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1132
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.