简体   繁体   English

如何从掷骰子中找到最小值和最大值?

[英]How can I find a minimum and maximum from a dice roll?

I want to find the minimum number, or maximum number, that I could possibly receive if put an X amount of dice and a X amount of dice sides. 我想要找到最小数目或最大数目,如果放置X数量的骰子和X数量的骰子面,我可能会收到。 I know that if I only roll one dice then the minimum would be the "minrange" and the maximum would be the "minrange" + "numOfSides", but if I were to roll multiple dice with X amount of sides; 我知道,如果我只掷一个骰子,那么最小值将是“最小范围”,而最大值将是“最小范围” +“ numOfSides”,但是如果我要掷出多个带有X个边数的骰子; how would I find the minimum/maximum number that could be generated? 我如何找到可以产生的最小/最大数量?

double rollDice(int numOfDice, int numOfSides, int divide, int minrange) {
    int i = 0;
    subtotal = 0;
    while (i < numOfDice) {
        roll = 0;
        roll = minrange + (rand() % numOfSides);
        subtotal += roll;
        i++;
    }
    return subtotal / divide;
}
#include <iostream>

void rollDice(int numOfDice, int numOfSides, int* minRange, int* maxRange) {
    *minRange = numOfDice;
    *maxRange = numOfDice * numOfSides;
    return;
}

int main() {

    int minRange;
    int maxRange;
    int noDice = 3;
    int noSides = 6;

    rollDice(noDice, noSides, &minRange, &maxRange);

    std::cout << "Minimum range for " << noDice << " dice with " << noSides << "sides is: " << minRange << std::endl;
    std::cout << "Maximum range for " << noDice << " dice with " << noSides << "sides is: " << maxRange << std::endl;

    return 0;
}

Firstly, slight mistake in your question, the maximum score for one dice would not be minRange + numOfSides, it would be numOfSides, and for an arbitrary number of dice the minimum score would be noDice * 1 = noDice and the maximum score would be noDice * noSides. 首先,您的问题中存在一些错误,一个骰子的最高分不是minRange + numOfSides,而是numOfSides,并且对于任意数目的骰子,最低分将是noDice * 1 = noDice,最高分将是noDice *无侧边。

Seen as you want to calculate the minimum and maximum totals of an arbitrary number of arbitrary sided dice, the best way to retrieve two values from a function, without creating a data structure or an array or an object is to use pointers. 好像您要计算任意数量的任意边骰子的最小和最大总数一样,从函数中检索两个值而不创建数据结构,数组或对象的最佳方法是使用指针。 By passing the address of the variables to store the minRange and maxRange using the & operator the rollDice function can directly access the chunk of memory used to hold those variables and set them to the appropriate values. 通过使用&运算符传递变量的地址以存储minRange和maxRange,rollDice函数可以直接访问用于保存这些变量的内存块并将它们设置为适当的值。

This is my first attempt at answering a question, hope it is what you are looking for. 这是我第一次尝试回答问题,希望它是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM