Is there a way around the while loop limit? [Solved]

Started by BrightBulb, Sun 03/11/2013 10:16:03

Previous topic - Next topic

BrightBulb

Hi everybody,

yesterday I just wrote a short programm to help me calculate the odds of dice rolling. As this is all the programm should do I did not concentrate on good scripting and just went using nested loops for each dice. The problem ist that I exceed the loop limit using more than 6 dice. Is there a quick way around this?

Thanks.

Ghost

A suggestion, post your code. Dice rolls are easy to do and there are a several ways of implementing them, so if you need a fix-up with your code, post it (nod).

If you're just using D6, a quick way to "roll one" is using a simple random command:

int die = Random(5)+1;

For several rolls an array can be used, together with a variable to store your "max amount of rolls".

int maxrolls = 100;
int results[100];

This will give you a huge storage for your dice rolls (nod)






BrightBulb

#2
I posted my code. There are different 6-sided dice with values like -3 to +3. The sum of all dice has to be at least +1 for a success. My goal is just to calculate the probabilty of such an success with a different number and types of dice.

Edit: Code deleted

DoorKnobHandle

#3
Yeah, as Ghost said, you are definitely not handling the problem in an efficient manner, given the problem. Nevertheless, you can use the noloopcheck keyword to disable the while loop iteration limit. Look it up in the manual to find out how it works exactly.

EDIT: Wrote this before you posted the code.

Code: "AGS"

bool diceRoll(int diceCount, int diceSides, int successMinimum)
{
    int sum = 0;
    int i = 0;
    while (i < diceCount)
    // for every dice
    {
        sum += Random(diceSides - 1) + 1;
        i++;
    }

    if (sum >= successMinimum * diceCount)
        return true;
    return false;
}


Now you can call the above function rollDice with the number of dice you want to roll, the number of sides they have and the success minimum. For example, calling rollDice(5, 20, 10) would roll 5 dice with 20 sides and, if they averaged to 10 or more, the function returns true, otherwise false.

This could be compressed to a single random command, but my function could be extended to support mixed dice types or visual representations of the individual dice rollinge etc.

Hope this helps.

Ghost

DoorKnob is right, that really is pretty inefficient. If I understand you correctly, you want to see if a roll in a -3 to 3 range results in a positive value? That is a 50% chance. So one command can give you the same effect:

if (Random(1) == 1) // yay, 50% chance success!

BrightBulb

#5
As I said I did not concentrate on clean scripting, I justed wanted a quick calculation. My description of my goals was simplified, sorry for that. Of course I wasn't scripting the whole thing to calculate a 50% chance. I just tried to point out that I'm not talking about ordinary d6, but actually about 5 different types of dice.

But thanks for the noloop command. That was what I was looking for.

Khris

Btw, instead of this:
Spoiler
if (control.ID==0) {iColor= 0;  iDiff = 7; iVorzeichen = -1;}
else if (control.ID==1) {iColor= 0;  iDiff = 6; iVorzeichen = -1;}
else if (control.ID==2) {iColor= 0;  iDiff = 5; iVorzeichen = -1;}
else if (control.ID==3) {iColor= 0;  iDiff = 4; iVorzeichen = -1;}
else if (control.ID==4) {iColor= 0;  iDiff = 3; iVorzeichen = -1;}
else if (control.ID==5) {iColor= 0;  iDiff = 2; iVorzeichen = -1;}
  else if (control.ID==6) {iColor= 0;  iDiff = 1; iVorzeichen = -1;}
  else if (control.ID==7) {iColor= 0;  iDiff = 0; iVorzeichen = 0;}
  else if (control.ID==8) {iColor= 0;  iDiff = 1; iVorzeichen=1;}
  else if (control.ID==9) {iColor= 0;  iDiff = 2; iVorzeichen=1;}
  else if (control.ID==10) {iColor= 0;  iDiff = 3; iVorzeichen=1;}
  else if (control.ID==11) {iColor= 0;  iDiff = 4; iVorzeichen=1;}
  else if (control.ID==12) {iColor= 0;  iDiff = 5; iVorzeichen=1;}
  else if (control.ID==13) {iColor= 0;  iDiff = 6; iVorzeichen=1;}
  else if (control.ID==14) {iColor= 0;  iDiff = 7; iVorzeichen=1;}
[close]

You can do this:
Code: ags
  iColor = 0;
  iDiff = control.ID - 7;
  iVorzeichen = -1 * (control.ID < 7) + (control.ID > 7);

Crimson Wizard

He seem to need absolute difference, so this perhaps:
Code: ags

iDiff = control.ID - 7;
if (iDiff < 0)
   iDiff = -iDiff;



SMF spam blocked by CleanTalk