473,909 Members | 4,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rolling dice

Let's say I have m dice having n sides, such that n^m is not going to bust
int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}.
What is a good way to represent this in c so that I can check cases by brute
force? EC
Sep 30 '06 #1
101 6461

"Elijah Cardon" <in*****@invali d.netwrote in message
news:12******** *****@corp.supe rnews.com...
Let's say I have m dice having n sides, such that n^m is not going to bust
int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}.
What is a good way to represent this in c so that I can check cases by
brute force? EC
Depends on your meory

typedef struct
{
int m;
int n;
int *roll; /* allocated dice with malloc */
} DICEROLL;

Is the obvious way if you have memory to burn.
However you already know all the possible dice in the roll set. So you can
generate the rolls from an index number by taking modulus and dividing.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Sep 30 '06 #2
Elijah Cardon wrote:
Let's say I have m dice having n sides, such that n^m is not going to bust
int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}.
What is a good way to represent this in c so that I can check cases by brute
force? EC
One way that comes to mind are n-adic numbers; start your results
at 0 (i.e. subtract 1 if necessary), then you get digits of a range
from 0 to n-1. This can be (using ^ to express powers) represented
as
a[m-1]*n^(m-1)+....+a[1]*n^1+a[0]*n^0.
I.e. the above can be represented as
(2-1)*216+(5-1)*36+(1-1)*6+(2-1)*1
In most cases, I'd rather use an array containing the digits -- for
many n, it is easier to debug. If you then really want to be able
to squeeze it into an int, then you can do it as soon as the rest
works.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 30 '06 #3
Malcolm posted:
typedef struct
{
int m;
int n;
int *roll; /* allocated dice with malloc */
} DICEROLL;

I strongly advocate the use of ALL CAPS for macros and for macros only.

--

Frederick Gotham
Sep 30 '06 #4
Elijah Cardon posted:
Let's say I have m dice having n sides, such that n^m is not going to
bust int as a datatype.

By "bust", do you mean overflow? The highest number reliably stored in an int
is 32767. If "n" is 6, then "m" can be as large as 5.

With m=4 and n=6, an outcome might be {2, 5, 1,
2}. What is a good way to represent this in c so that I can check cases
by brute force? EC

I don't know what you mean by "check cases". The programming language is
invariably spelled with an uppercase C, just so you know.

--

Frederick Gotham
Sep 30 '06 #5

"Malcolm" <re*******@btin ternet.comwrote in message
news:RK******** ************@bt .com...
>
"Elijah Cardon" <in*****@invali d.netwrote in message
news:12******** *****@corp.supe rnews.com...
>Let's say I have m dice having n sides, such that n^m is not going to
bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1,
2}. What is a good way to represent this in c so that I can check cases
by brute force? EC
Depends on your meory

typedef struct
{
int m;
int n;
int *roll; /* allocated dice with malloc */
} DICEROLL;
For the moment, I want to stay away from mallocs and structs. I think the
above approach would favor object orientation.
Is the obvious way if you have memory to burn.
However you already know all the possible dice in the roll set. So you can
generate the rolls from an index number by taking modulus and dividing.
Rolling the dice is what I'm not going to do. I'm going to enumerate the
outcomes. Memory is plentiful, and stdout will never run out of paper.
#define m 4
#define n 6

I don't see what good the first #define will do me when I need to hard code
the number of '[n]' in the statement

int a[n]a[n]a[n]a[n];

Any comment appreciated. EC
Sep 30 '06 #6
Elijah Cardon posted:
Rolling the dice is what I'm not going to do. I'm going to enumerate
the outcomes.

Bitwise manipulation might be handy for minimising memory consumption. For
instance, if a dice has 6 sides, then we only need 3 bits to log each
individual roll:

000 == Rolled 1 on the dice.
001 == Rolled 2 on the dice.
010 == Rolled 3 on the dice.
011 == Rolled 4 on the dice.
100 == Rolled 5 on the dice.
101 == Rolled 6 on the dice.

For even more conservative use of memory, we can find a multiple of 6 which
is also a integral power of 2 (unfortunately though, any such number is
quite large).

This problem would be far better solved with an OO language, but if you
want to use C, then so be it. Maybe something like:

#include <stddef.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>

typedef struct EnumInfo {
unsigned bits_per_outcom e;
size_t quant_bytes;
char unsigned *p;
} EnumInfo;

unsigned BitsNeeded(unsi gned combinations)
{
/* I'm pretty sure there's a far
better way of implementing this
function.
*/

int const assert_dummy = (assert(!!combi nations),0);

unsigned bits = 1;

--combinations;

while(combinati ons >>= 1) ++bits;

return bits;
}

EnumInfo EnumerateResult s(unsigned const throws,unsigned const sides)
{
EnumInfo info;

info.bits_per_o utcome = BitsNeeded(side s);

info.quant_byte s = info.bits_per_o utcome * (throws/CHAR_BIT +1);
/* A few bytes wasted */
/* We might want a few checks to make sure
the size_t hasn't overflowed. */

info.p = malloc(info.qua nt_bytes);

/* Write the results to memory */

return info;
}

It will be a little tricky if CHAR_BIT isn't evenly divisible by
"bits_per_outco me" (which will happen quite a lot for 6-sided die on a
system with 8-Bit bytes). The complexity could be greatly reduced however
if you used an OO language which allowed you to implement a BitHandle which
could encapsulate the nitty-gritty of the bit manipulation away from your
other code -- but still, it's achievable in C.

--

Frederick Gotham
Sep 30 '06 #7

"Michael Mair" <Mi**********@i nvalid.invalidw rote in message
news:4o******** ****@individual .net...
Elijah Cardon wrote:
>Let's say I have m dice having n sides, such that n^m is not going to
bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1,
2}. What is a good way to represent this in c so that I can check cases
by brute force? EC

One way that comes to mind are n-adic numbers; start your results
at 0 (i.e. subtract 1 if necessary), then you get digits of a range
from 0 to n-1. This can be (using ^ to express powers) represented
as
a[m-1]*n^(m-1)+....+a[1]*n^1+a[0]*n^0.
I.e. the above can be represented as
(2-1)*216+(5-1)*36+(1-1)*6+(2-1)*1
In most cases, I'd rather use an array containing the digits -- for
many n, it is easier to debug. If you then really want to be able
to squeeze it into an int, then you can do it as soon as the rest
works.

#include "stdio.h"
#define n 6

int main(int argc, char* argv[])
{
int i1, i2, a[n][n];

for (i1 = 0; i1 < n; i1 ++)
{
for (i2 = 0; i2 < n; i2 ++)
{
/*what here */
}
}
printf("Hello World!\n");
return 0;
}
I was thinking that arrays were the way to go, but as I start to write the
control loops, it's the dummies themselves that I want. And I would
re-write the loops to go from 1 to n, as opposed to the matrix-friendly 0 to
n-1 . Where I have the comment, I could test for whether the dummies add to
seven and keep a counter of those that do, and those that don't. I would
then have the probability I'm looking for.

What I don't see is how I would take this approach and have a program that
isn't totally hard-coded for every dimension. EC
Sep 30 '06 #8
Frederick Gotham wrote:
For even more conservative use of memory, we can find a multiple of 6 which
is also a integral power of 2 (unfortunately though, any such number is
quite large).
If by power of 2 you mean a number of the form
2^k then no such number can be a multiple of 6.

Sep 30 '06 #9
Frederick Gotham wrote:
[...]
For even more conservative use of memory, we can find a multiple of 6 which
is also a integral power of 2 (unfortunately though, any such number is
quite large).
I can think of only one such integer, but it isn't very
large at all ...

--
Eric Sosman
es*****@acm-dot-org.invalid
Sep 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1970
by: lawentzel | last post by:
I am new to PHP and am an avid RPG gamer. I am looking to use PhpMyChat to be able to game with friends all over the US. With that having been said, PhpMyChat seems to offer a lot of what I am looking for. It allows you to send a message to individual players (chatters). It allows for actions using "/me". The only thing it really needs is the capability to do a dice roll. Can someone point me to a site which might have some examples...
4
6390
by: dpp4669 | last post by:
I need help with writing a program to use randmax and the standard deviation when u roll the dice 10000 times I know you will need the standard dev. and the average where do i put the randmax and do i use 1+6 after it ? LOST!
17
9624
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my simulation, one or more of my sums is zero, which is highly unlikely. So I'm sure there's a bug in my code. But I can't tell what it is, even after re-reading it several times. Can someone give me a hint? Here is my code:
4
4138
by: vegtard | last post by:
simple question. is it possible to write a script that rolls a six sided dice? if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score? if you want to know, i am working on a script to create a dungeons and dragons character :)
2
3404
by: sunnydude | last post by:
Anybody know how to write a 3 dice rolling program like this Sample output Welcome to the dice roller! First of all, please enter a seed for the random number generator. This should be a positive integer: 3 How many times would you like me to roll the 3 dice?
2
6493
by: cowboyjeff05 | last post by:
the question is 1) build a Die class (6 sided). Then create a driver that tests your class. Start with one die in your driver then add a second die and notify the user and count when he or she rolls 7, 11, or a double. When the user is done rolling, output the results of the session. Create a child class from the Die class called MultisidedDie. This die should allow the programmer using the class to decide how many sides the die has. ...
0
10035
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11346
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8097
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7248
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5938
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4774
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
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.