what kind of dice do I need for dnd code example

Example 1: code to calculate dice score

import numpy as np
np.random.seed(0)
true = np.random.rand(10, 5, 5, 4)>0.5
pred = np.random.rand(10, 5, 5, 4)>0.5

def single_dice_coef(y_true, y_pred_bin):
    # shape of y_true and y_pred_bin: (height, width)
    intersection = np.sum(y_true * y_pred_bin)
    if (np.sum(y_true)==0) and (np.sum(y_pred_bin)==0):
        return 1
    return (2*intersection) / (np.sum(y_true) + np.sum(y_pred_bin))

def mean_dice_coef(y_true, y_pred_bin):
    # shape of y_true and y_pred_bin: (n_samples, height, width, n_channels)
    batch_size = y_true.shape[0]
    channel_num = y_true.shape[-1]
    mean_dice_channel = 0.
    for i in range(batch_size):
        for j in range(channel_num):
            channel_dice = single_dice_coef(y_true[i, :, :, j], y_pred_bin[i, :, :, j])
            mean_dice_channel += channel_dice/(channel_num*batch_size)
    return mean_dice_channel

def dice_coef2(y_true, y_pred):
    y_true_f = y_true.flatten()
    y_pred_f = y_pred.flatten()
    union = np.sum(y_true_f) + np.sum(y_pred_f)
    if union==0: return 1
    intersection = np.sum(y_true_f * y_pred_f)
    return 2. * intersection / union

print(mean_dice_coef(true, pred))
print(dice_coef2(true, pred))

# 0.4884357140842496
# 0.499001996007984

Example 2: dice3

#include<iostream>
#include<cstdlib>
#include<ctime> 
#include<iomanip>
#include<vector>
#include<cmath>
#include<algorithm> 
int main( ) {
    std::cout << "enter seeding number: " ; 
    int num_seed = 0 ;
    std::cin >> num_seed ;
    srand( num_seed ) ;
    std::cout << "enter numbers of tries: ";
    int tries = 0 ;
    std::cin >> tries ;
    std::cout << "enter numbers of asterikes: "; 
    int asterikes = 0 ;
    std::cin >> asterikes ;
    double sum_of_three_dices , average_of_three_dices,sigme_dices_powered, variation, standard_dev = 0.0 ;
    std::vector<size_t> vec_of_three_dice(19,0);
    for(size_t i = 0; i < tries ; i++)
    {
        int dice_one = rand()%6 + 1 ;
        int dice_two = rand()%6 + 1 ;
        int dice_three = rand()%6 + 1 ;
        vec_of_three_dice[dice_one + dice_two + dice_three] += 1 ;
        sum_of_three_dices += dice_one + dice_two +dice_three ;
        average_of_three_dices = sum_of_three_dices / tries ;
        sigme_dices_powered += pow(dice_one + dice_two + dice_three ,2);
        variation = sigme_dices_powered/tries - pow(average_of_three_dices,2);
        standard_dev = sqrt(variation);
    }

    average_of_three_dices = sum_of_three_dices / tries ;
    std::cout << "average_of_three_dices is: " <<std::fixed << std::setprecision(6) << average_of_three_dices << std::endl;                                                                                                                                                                                                                          /* 29421  796842 */
    std::cout << "standard deviation is: " <<std::fixed << std::setprecision(6) << standard_dev << std::endl;
    std::cout << std::endl;
    
    int max_value = *max_element(vec_of_three_dice.begin(),vec_of_three_dice.end());
    for(size_t i = 3; i <vec_of_three_dice.size() ; i ++ ){
        if(i < 10 ){
            std::cout <<std::setw(2) <<std::setfill(' ') << i<< " : " <<"("<<std::setw(4)<<std::setfill(' ')<<vec_of_three_dice.at(i) << ")" ;
        }else{
            std::cout << i << " : " <<"("<<std::setw(4)<<std::setfill(' ')<<vec_of_three_dice.at(i) << ")";
        }
       for(size_t j = 0; j <= static_cast<size_t>(vec_of_three_dice.at(i) * asterikes)/max_value ; j++ ){
           std::cout << "*";
       }
        std::cout << std::endl;


    } 
    return 0 ;
}

Tags:

Cpp Example