Jump to content

Recommended Posts

Posted

The function I need is one which calculates the number of permutations which add to "n", using "l" six sided dice.

 

for example if i was using a single die (l=1), and i wanted to know how many ways there were to get a 4 (n=4), the function would return 1. If i were using 2 dice (l = 2) and i wanted to know how many ways there were to get a 7 (n = 7), the function would return 6 (1,6; 6,1; 5,2; 2,5;4,3; 3,4).

 

I know it's going to be based on the old nPr function but it's more complex than that i think.

 

I intend to incorporate it into an excel spreadsheet to demonstrate entropy and numbers of microstates.

Posted

I don't know a solution to your problem out of my head but there is one thing that comes to my mind:

attachment.php?attachmentid=2522&stc=1&d=1275338025

flipacoin.jpg

Posted

Well, I was serious about the idea with the coin, in case that wasn't clear. With coins, the distribution is the binomial distribution which excel should be able to handle (and coins are still valid for showing the effect of entropy).

Posted (edited)

Here is a way to compute the number of ways to generate all possible sums of m tosses of a die with n faces. Use the generating function, which for a fair die of n faces is [math]g(z) = z+z^2+\cdots+z^n[/math]. Next compute [math]g(z)^m[/math], where m is the number of tosses. Finally, read off the polynomial coefficients. For example, the number of ways to get a total of three is the coefficient of [math]z^3[/math] in [math]g(z)^m[/math].

 

Here is a perl script that computes and prints this:

#!/usr/bin/perl -w
use strict;
sub mult_poly($$);
my ($tosses,  # Number of tosses of the die
   $faces,   # Number of faces on the die
   $gen,     # Generating polynomial g(z)
   $pow);    # g(z)^m

# Get command line arguments. Usage is <script_name> [#tosses [#faces]]
$tosses = scalar @ARGV ? shift : 2;
$faces  = scalar @ARGV ? shift : 6;

# Construct the generating function, g(z)=z+z^2+...i+z^n, n=#faces.
$gen = [0, (1) x $faces];

# Compute g(z)^m, m=#tosses
$pow = [1];
$pow = mult_poly $pow, $gen foreach (1 .. $tosses);

# Print results.
printf "%3s %9s\n", qw(N Count);
printf +("%3d %9d\n", $_, $pow->[$_]) foreach ($tosses .. $#$pow);

# Sanity check; the printed sum should be n^m.
my $s = 0;
map {$s += $_} @$pow;
printf "Sum %9d\n", $s; 


# Multiply two polynomials.
sub mult_poly ($$) {
  my ($p, $q) = @_; 
  my $prod = [(0) x ($#$p + $#$q + 1)];
  for (my $ip = 0; $ip <= $#$p; $ip++) {
     for (my $iq = 0; $iq <= $#$q; $iq++) {
        $prod->[$ip+$iq] += $p->[$ip] * $q->[$iq];
     }   
  }   
  return $prod;
}


Merged post follows:

Consecutive posts merged

The above might overkill if you don't want all possible combos. If all you want the number of ways to achieve a specific sum s given t tosses of a die with f faces,

 

Ooops. What I posted for this individual problem doesn't work. Deleted.

 

For now use the algorithm in the first half of this multi-post.

Edited by D H
Consecutive posts merged.
Posted
Here is a way to compute the number of ways to generate all possible sums of m tosses of a die with n faces. Use the generating function, which for a fair die of n faces is [math]g(z) = z+z^2+\cdots+z^n[/math]. Next compute [math]g(z)^m[/math], where m is the number of tosses. Finally, read off the polynomial coefficients. For example, the number of ways to get a total of three is the coefficient of [math]z^3[/math] in [math]g(z)^m[/math].

 

Here is a perl script that computes and prints this:

#!/usr/bin/perl -w
use strict;
sub mult_poly($$);
my ($tosses,  # Number of tosses of the die
   $faces,   # Number of faces on the die
   $gen,     # Generating polynomial g(z)
   $pow);    # g(z)^m

# Get command line arguments. Usage is <script_name> [#tosses [#faces]]
$tosses = scalar @ARGV ? shift : 2;
$faces  = scalar @ARGV ? shift : 6;

# Construct the generating function, g(z)=z+z^2+...i+z^n, n=#faces.
$gen = [0, (1) x $faces];

# Compute g(z)^m, m=#tosses
$pow = [1];
$pow = mult_poly $pow, $gen foreach (1 .. $tosses);

# Print results.
printf "%3s %9s\n", qw(N Count);
printf +("%3d %9d\n", $_, $pow->[$_]) foreach ($tosses .. $#$pow);

# Sanity check; the printed sum should be n^m.
my $s = 0;
map {$s += $_} @$pow;
printf "Sum %9d\n", $s; 


# Multiply two polynomials.
sub mult_poly ($$) {
  my ($p, $q) = @_; 
  my $prod = [(0) x ($#$p + $#$q + 1)];
  for (my $ip = 0; $ip <= $#$p; $ip++) {
     for (my $iq = 0; $iq <= $#$q; $iq++) {
        $prod->[$ip+$iq] += $p->[$ip] * $q->[$iq];
     }   
  }   
  return $prod;
}


Merged post follows:

Consecutive posts merged

The above might overkill if you don't want all possible combos. If all you want the number of ways to achieve a specific sum s given t tosses of a die with f faces,

 

Ooops. What I posted for this individual problem doesn't work. Deleted.

 

For now use the algorithm in the first half of this multi-post.

 

I'm fairly sure your method will work, but i'm not sure if excel can handle it. Basically I have developed an excel spreadsheet which generates 256 random integers between 1 and 6. It then takes the sum of however many dice you want to throw (user input), and plots the result on a frequency graph. I have written/recorded some macros which allow me to throw the dice multiple times (up to 10,000), although i don't understand visual basic at all. What i want to do is compile a list of possible totals for a specific number of dice (perhaps not as many as 256, but a large amount) and the number of ways of throwing each possible total.

 

Can it be done in excel?

Posted
Here is a way to compute the number of ways to generate all possible sums of m tosses of a die with n faces. Use the generating function, which for a fair die of n faces is [math]g(z) = z+z^2+\cdots+z^n[/math]. Next compute [math]g(z)^m[/math], where m is the number of tosses. Finally, read off the polynomial coefficients. For example, the number of ways to get a total of three is the coefficient of [math]z^3[/math] in [math]g(z)^m[/math].

 

Here is a perl script that computes and prints this:

#!/usr/bin/perl -w
use strict;
sub mult_poly($$);
my ($tosses,  # Number of tosses of the die
   $faces,   # Number of faces on the die
   $gen,     # Generating polynomial g(z)
   $pow);    # g(z)^m

# Get command line arguments. Usage is <script_name> [#tosses [#faces]]
$tosses = scalar @ARGV ? shift : 2;
$faces  = scalar @ARGV ? shift : 6;

# Construct the generating function, g(z)=z+z^2+...i+z^n, n=#faces.
$gen = [0, (1) x $faces];

# Compute g(z)^m, m=#tosses
$pow = [1];
$pow = mult_poly $pow, $gen foreach (1 .. $tosses);

# Print results.
printf "%3s %9s\n", qw(N Count);
printf +("%3d %9d\n", $_, $pow->[$_]) foreach ($tosses .. $#$pow);

# Sanity check; the printed sum should be n^m.
my $s = 0;
map {$s += $_} @$pow;
printf "Sum %9d\n", $s; 


# Multiply two polynomials.
sub mult_poly ($$) {
  my ($p, $q) = @_; 
  my $prod = [(0) x ($#$p + $#$q + 1)];
  for (my $ip = 0; $ip <= $#$p; $ip++) {
     for (my $iq = 0; $iq <= $#$q; $iq++) {
        $prod->[$ip+$iq] += $p->[$ip] * $q->[$iq];
     }   
  }   
  return $prod;
}


Merged post follows:

Consecutive posts merged

The above might overkill if you don't want all possible combos. If all you want the number of ways to achieve a specific sum s given t tosses of a die with f faces,

 

Ooops. What I posted for this individual problem doesn't work. Deleted.

 

For now use the algorithm in the first half of this multi-post.

 

its a some kind of power series expansion

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.