Skip to content

Question about different die rolls.

I have a script with these lines in it.
int nInt;
nInt = d20();
if (nInt==1)

It works fine as is. Also works if changed to d100. My question being. Why does it not compile if changed to d40?

Comments

  • DazDaz Member Posts: 127
    It does not compile because there is no d40() function, there's d2(), d3(), d4(), d6(), d8(), d10(), d12(), d20() and d100().

    If you want to have a d40() roll you can do the following:
    int nInt = Random(40) + 1;
    

  • TarotRedhandTarotRedhand Member Posts: 1,481
    edited February 2023
    Alternatively -

    int nInt;
    nInt = d20(2) ;
    if (nInt==1)
    
    All the dice functions take an int parameter that specifies the number of dice to roll. With that code it will be impossible to make a roll of 1 but if you subtract 1 from the result of d20(), 40 becomes impossible to roll. That makes @Daz's code more accurate.

    If you are on Windows you can check most of EE's functions in the searchable, offline version of the Lexicon.

    TR
  • MelkiorMelkior Member Posts: 204
    Untested, except in my head. Check to see if it works before implementing.
    // Generalised function which will roll a die with any integer number of sides any number of times.
    // *Caution* May cause TMI (Too Many Instructions) errors for very high values of iTimes.
    // Rolls a dice with iRoll sides iTimes times and returns the total.
    int DNum(int iRoll=1,int iTimes=1)
    {
      if(iRoll<1 || iTimes<1) return 0;// Return 0 on error.
      int iResult=0;
      while (iTimes>0)
      {
        iResult+=(Random(iRoll)+1);
        iTimes--;
      }
      return iResult;
    }
    
    Examples:
    int iD40=DNum(40,1);
    Returns a random integer between 1 and 40
    int iD40by2=DNum(40,2);
    Returns a random integer between 2 and 80
  • ProlericProleric Member Posts: 1,316
    edited February 2023
    Alternatively -

    int nInt;
    nInt = d20(2) ;
    if (nInt==1)
    
    All the dice functions take an int parameter that specifies the number of dice to roll. With that code it will be impossible to make a roll of 1 but if you subtract 1 from the result of d20(), 40 becomes impossible to roll. That makes @Daz's code more accurate.

    If you are on Windows you can check most of EE's functions in the searchable, offline version of the Lexicon.

    TR

    2d20 is not the same as d40. Amongst other things, it can never be 1.
  • TarotRedhandTarotRedhand Member Posts: 1,481
    @Proleric I said that already.

    TR
  • TheTinmanTheTinman Member Posts: 74
    Would leaving off the +1 give a d40 die roll?
    int nInt = Random(40);
    

  • Christian79Christian79 Member Posts: 48
    TheTinman wrote: »
    Would leaving off the +1 give a d40 die roll?
    int nInt = Random(40);
    

    No. In most programming languages random function uses a float type and starts at 0. When casting to integer the float gets rounded down. So above code gives you a number from 0 to 39.

    Also in general rolling multiple dice and adding them gives you a complete different probability distribution than rolling a single larger die. The more dice you throw the more likely you end up with a result in the middle of the spectrum. For example 2D6 gives you a higher chance for a 7 than the other results whereas a D12 treats all possible results equally.
  • TheTinmanTheTinman Member Posts: 74
    Got it sorted. Thanks for all the replies. You have all taught me a lot. B)
Sign In or Register to comment.