C :: Simulate Roll Of A Dice - How To Use Rand Function

Sep 23, 2014

I saw a program in which it uses rand like this:-

d1=rand() % 6+ 1;

where, d1 is any integer. The program is to simulate the roll of a dice. The whole program is this:-

Code:

#include<stdio.h>
#include<stdlib.h>
main() {
int i;
int d1, d2;
int a[13];

[Code] .....

View 10 Replies


ADVERTISEMENT

C++ :: Dice Game - Receive Same Values When Roll Function Get Call Twice For Same Object

Nov 7, 2013

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace std;
class Dice {

[Code] ....

Every time the roll_dice function get call twice for the same object, i receive the same dice values.

View 4 Replies View Related

C++ :: Five Dice Roll - Read Out Average And Sum Of Rolls

Feb 7, 2013

My issue is that my program dies whenever it attempts to readout the average and sum of the rolls. I think I need to include the size function in here somewhere but am not sure how to go about it. Possibly scrap the "do while" loop and go with something else, or just am completely missing something.

Here's the code:

#include<iostream>
#include<random>
#include<ctime>
#include<vector>
#include<cstdlib>
#include<iomanip>
using namespace std;
int sum5(int d1, int d2, int d3, int d4, int d5);
int DiceRoll();

[Code] ....

View 3 Replies View Related

C :: Simulating Random Dice Roll As A Basis For Chutes And Ladders Game

Jan 24, 2014

I am trying to simulate a random dice roll as a basis for a chutes and ladders game

Code:
int main {
int i = 0, diceroll;
while (i>5) {
diceroll = 1+rand()%6;
i++;
}
return 0;
}

However the numbers get don't seem to be random, it always starts off with 6,6,5.. then its random.

View 10 Replies View Related

C++ :: Using Rand Function And Resetting Seed?

Nov 9, 2014

So I've actually used rand() happily without any problems so far, but now I faced a problem. I have an AI loop that initialized a random number between 1 and 2 at the beginning, so something like this:

while (condition) {
int random = rand()% 1 + 2;
if (random == 1) {

[Code] ....

So this doesn't really work since it doesn't actually assign a new random value. I came across srand(time(NULL)), which resets the seed, but I didn't quite get when to use it. Do I have to call it in every cycle of the loop? I'm not completely sure how this works.

View 2 Replies View Related

C++ :: Using Rand Function In While Loop And Output Using Switch Statement

Jun 12, 2014

Basically I'm supposed to use a while loop to generate a random number and use a switch statement to output the appropriate information. I feel like I'm missing a few things that are very simple.

The errors are:
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
warning C4700: uninitialized local variable 'randomNumber' used

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
int main(){
int i = 0;
unsigned int randomNumber;

[Code] .....

View 2 Replies View Related

C/C++ :: Sum Function Of For Loop For Dice Generator

Oct 15, 2014

I have written the code below that allows the user to input any sided dice and any number of rolls. The program will print out the output in sequence. I need to sum these outputs into one number, but I am having trouble writing a sum function for this.

//This Program Creates the Sum of a Number of Dice Rolls of the Same Size  

#include <stdio.h> 
#include <math.h>
#include <time.h> 
int rolldie();   
int main(){ 
int x, n, a; //'x' n=number of rolls, a=number sided dice

[Code] ....

The output looks something like this currently:

Please Enter the Number Sided Dice Followed by the number of Rolls... 12 10
4 12 2 10 12 5 5 12 12 5 >Exit code: 0

View 2 Replies View Related

C :: Snakes And Ladder Program - Dice Function To Switch Between Players

Mar 25, 2013

I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the dice to work in the way I want it to work. I want the dice to work like this :

Before each player throw the dice they must start at 0.
Each player must throw a 6 on the dice to move on the board.
If a player threw a 6 on the dice, that player can throw again.

But I ended up with two seperate dice, one for each player (game is only for two players).And when I run the program, both players don't start at 0. And when I throw the dice, both players move at the same time but with different values.

If one of my players threw a 6, they just keep on throwing until someone wins the game. I tried to use a switch and if statements but I couldn't get it right. And so I did this :

Code:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define SpaceBar 32// 32 is an ASCII value for a spacebar

[Code] ....

View 5 Replies View Related

Visual C++ :: Dice Game - Creating Pass By Value Function Reference

Nov 27, 2014

i have a project where i create a dice game, the user rolls 2 dice and the computer roles 2 dice. the player that wins 3 out of 5 rolls wins the game. I have completed the requirements, but i wanted to create a pass by value function for "void Dice()", I'm not too sure how that works?

Code:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//creating my variables and their values
int compinput;

[Code] .....

View 5 Replies View Related

C++ :: Avoiding Rand To Generate Same Numbers

Mar 25, 2014

I tryed to create program I wich u can guess number, I made some if to prevent inputting 2 times the same number It works. I tryed to apply the same process for generating

#include <cstdlib>
#include <iostream>
#include <stdlib.h>

[Code]....

View 6 Replies View Related

C++ :: Algorithm To Roll A Rectangle

Sep 17, 2014

I'm trying to draw and pivot a rectangle. How to get the new coordinates if I turn it 90 degree for example?

From this:

To this:

This is not good because it will reduce it in size.

Code:
int size = 50;
if(b) // draw rectangle
{
POINT points[4] =
{{xCoord, yCoord},

[Code] ....

View 7 Replies View Related

C++ :: Make The Text Roll?

Aug 31, 2013

I have the code to make the text roll. But the problem is that the application can't execute other instructions coz its busy in the infinite loop of the marquee. How do i make the application to execute its normal instructions as well as roll the text simultaneously

View 7 Replies View Related

C++ :: Normal Rand Generator - Class Functions

Sep 19, 2014

So I have an object of class NRG (Normal Rand Generator) which takes, as an argument to it's constructor, an object of class RG (Rand generator).

template<typename RG>
class NRG {
RG rg;
public:
NRG(RG r):rg(r){}
double operator()();

An object of this class will return a normal random when it's member function operator()() is called:

template<typename RG>
double class NRG::operator()() {
static int flag = 0;
static double N2 = 0.0;
if(flag==0)

[Code] ....

However, when I run this I get an error which says:

C:UsersavadhootDesktopb.cpp|69|error: 'template<class RG> class NRG' used without template parameters|
C:UsersavadhootDesktopb.cpp|69|error: expected identifier before 'operator'|
C:UsersavadhootDesktopb.cpp|69|error: two or more data types in declaration of 'operator()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

View 3 Replies View Related

C++ :: Card Rand And Suit - Adding Dynamic Arrays

May 1, 2013

I'm writing a program with two classes, one that contains a card's rank and suit and the other contains pile of these cards. I'm having trouble when it comes to adding a pile to another pile...here's what I have so far:

class Card //Card class declaration
{
private:
int rank; //invoking rank
char suit; //invoking suit
public:
void setCard(int r,char s); //determines card suit/rank

[Code] ....

I understand that I'm calling my pile arrays incorrectly, but I'm not sure why.

View 1 Replies View Related

C++ :: Why Projects Always Roll Their Own Data Structures / Utility Classes

Jun 9, 2014

Why certain projects always roll their own data structures/utililiy classes rather than using the C++ standard libraries? such as linked lists and queues? Is there a particular reason for this?

View 11 Replies View Related

C :: If Statements For Dice Rolling

Oct 9, 2014

I'm very new to C Programming and am doing some homework for my intro to C class. "There is a game where one can play and bet money on. Two dice are rolled and the numbers that appear on the dice are added together. If the total is 7 or 11, then the user wins. If the total is 2 or 12, then the user loses. If a different total appears. then the user gets their money back."

I have written the program below:

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main()
{
int d1,d2,sum;
d1=0;
d2=0;
}

[code]...

However, after finishing the program, I realized that I NEEDED to use random numbers!!! How do I make my program generate random numbers within a range of 1-12? Also, how do I make my program generate NEW random numbers EVERY TIME instead of ONCE??

View 1 Replies View Related

C++ :: Dice Simulator - Floats Rounding Up

Oct 28, 2013

I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).

Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.

View 5 Replies View Related

C++ :: Random Generator For Dice Rolling?

Oct 29, 2013

How would you code a simple random generator for dice rolling?

View 2 Replies View Related

C++ :: Adding Up Values Of Dice Rolls In A While Loop

Feb 19, 2013

I'm having a problem adding up the values of the dice rolls in a while loop. The program will only store the last value so how would I get it to store multiples values? Here is my program thus far. I would try an array but we haven't gotten to that yet.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int die_1=0, die_2=0, total_score1, total_score2, player1score;
char enter;

[Code] ....

What operators or equation I should use to accomplish this?

View 7 Replies View Related

C++ :: Create A Dice Rolling Counter Using Classes?

Dec 1, 2014

I'm trying to create a program that rolls a dice a few thousand times and print out how many times it lands on each face and then print out a graph using x's as a visual representation using classes.

output looks like this:
1 --- 2000 xxxxxxxxxxxxxxxxxxxxxxxxx
2 --- 1950xxxxxxxxxxxxxxxxxxxxxxxx
.
.
6 --- 2050 xxxxxxxxxxxxxxxxxxxxxxxxxxx

I need a method called int count(int face) inside my class that returns the number of times a particular face has appeared, I just don't know how to incorporate this. I think I should use a constructor and destructor but i'm new with those and knowing when to actually use them.

Here is what I have so far that shows how it should look when printed, just struggling to convert using a class.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

[Code]....

View 2 Replies View Related

C# :: Creating A Class And Methods For Dice Game?

Mar 21, 2015

The only difficulty im having is creating a class and methods & being able to access them from my win form app. Can i get a few tips on the do's and donts of creating classes / methods and accessing them from form app.

This is what i have put together so far.

public partial class Form1 : Form {
private Image[] dicePics;
private int[] diceNum;
private Random randomize;
public Form1() {
InitializeComponent();

[code]....

View 3 Replies View Related

C :: Program Where A Computer And Player Take Turns Rolling Dice

Feb 19, 2013

I am making a program where a computer and player take turns rolling dice.

There seems to be an issue somewhere in my loop where when the player selects from the possible dice choices, it rolls for the computer and does not select the choice and it immediately becomes the player's turn again.

I am trying to get it to where when the computer rolls the dice, it should display the list of possible values, and ask the user to hit the enter key in order for the computer to choose which value to select using the following command:

Code:
while ( getchar() != '
' ) ; I've tried it a hundred times and no matter where I put the command it still skips the computer's turn.

Below is my code:

Code:
while (compscore < 99 && playerscore < 99){
printf("You have rolled the following pair of dice:
");
a =("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL);
b =("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL);
multiply = a * b;

[Code] ....

View 11 Replies View Related

C/C++ :: Rolling Multiple Dice - Compare Total Sums

Feb 1, 2015

I am building the game risk and I want to start off with a dice rolling mechanism. In risk 5 die are needed so I want to know the most efficient way of churning out the code for this. Since the players have the options of rolling 1, 2 or 3 die (max 3 for attacker, and max 2 for defender) I was thinking something like this after searching the web for a few different options:

const int minNum = 1;
const int maxNum = 6;
int numberGenerator(){
int x;
x = minNum + rand() % (maxNum - minNum + 1);

[Code] ....

Since I will be asking the player the option to choose how many die to roll, is this the most efficient way? I want to compare the total sums after the die are rolled. If they choose 3 dice (the attacker) I will simply call the first 3 methods and either 1 or 2 for the defender.

View 14 Replies View Related

C/C++ :: Dice Rolling - Keeping Track Of How Many Times A Number Appears?

Sep 27, 2014

I wrote a code that mimics a dice, my code so far will ask for the user to input a number at which they want to hold or no longer throw the dice, it will also ask the user how many roll simulations they would like to do.

If a 1 is rolled the turn is over and the score is 0

After this input is recieved the code goes into a loop until the values have been met.

I would to add probability to my code so I need to know how many times a certain number was rolled and this is where I am stuck.

An example of how I would like my code to work, let's suppose hold at number is 17

We role the dice until we roll a 17 or larger, but if we roll a 1, then our score is 0.

Let's suppose the number of simulations is 5 and for simulation 1 we roll

2 + 5 + 6 + 4 = 17

and for simulation 2 we roll

5 + 4 + 1, but since we rolled a 1, the score is 0,

for simulation 3 we roll

3 + 4 + 5 + 6 = 18

for simulation 4 we roll

4 + 4 + 6 + 6 = 20

for simulation 5 we roll

3 + 4 + 5 + 5 = 17 so that means we got

0: 1 time 1/5 = .20
17: 2 times 2/5 = .40
18: 1 time 1/5 = .20
19: 0 times 0/5 = .00
20: 1 time 1/5 = .20
21: 0 times 0/5 = .00
22: 0 times 0/5 = .00

In other words the idea is to roll a 17 or larger (or whatever the hold number happens to be), but if you roll a 1, then your score is a 0.

#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int dieRoll();

[Code] ....

View 4 Replies View Related

C++ :: Dice (with Varying Number Of Sides) Program To Calculate Percentage Of Results

Oct 28, 2013

I am trying to code and compile a program that requests a uses to first inpu the number of sides they wish for a dice to have and secondly input the number of dice throws they would like to calculate the value for. The program will print an error message and default the number of sides the dice has to 5 if the number entered is below 4. Finally it will print out a percentage figure for each value that results from all the the dice rolls.

I have attached my header, driver and class files below along with accompanying error messages below them

// file name Exercise.cpp
#include <iostream>
#include "Die.h"
using namespace std;
int main() // the main program begins here
{
char restart = 'y'; // declare variables
double numRolls = 0.0;
int diceTot = 0;

[Code] .....

View 5 Replies View Related

C :: UDP Application Simulate Packet Loss

Aug 12, 2014

have udp server-client application written in C. On the client side packet loss is detected using recvfrom function and sequence numbers of packets. How can I now simulate dropped packet's on the client side, for example if server is sending 1000 packet I want to drop 20% of them? I want to do this in the code, not for example using ip tables or WANEM or something like that. And one more thing, I have few clients and I want that they can dropped different packets, not the same one.

Code:
while(1){
nbytes = recvfrom(socket, buffer, MAX_SIZE, 0, (struct sockaddr *) &srv_addr, &addrlen);
if (nbytes != -1) {
// packet is received

}else{
//packet is not received
}
}

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved