[Python] How to generate random numbers for either/or choice of events

jkp1187

Unindicted Co-Conspirator
Joined
Aug 29, 2004
Messages
2,496
Location
Pittsburgh, Pennsylvania
Hello!

Another question for you wonderful people: how can I generate a random number such that, depending on the results, one and only one of several possible additional events will trigger?

If I can go back to my paper & pencil roots, I just want to set up a simple single-iteration D100 roll, then if the result is 1-33, trigger event A, if the result is 33-67, trigger event B, if the roll is 68-100, take no action.

I assume from poking around that getSorenRandNum is going to factor into the answer. I didn't see any examples in the existing code for what I wanted to do, though.... (I tried! I really looked this time!)

In the events XML files, there is a chance to trigger additional events...but it doesn't appear that you can set those up in an either/or fashion. You can set up multiple possible additional events, but the % chance for triggering is rolled individually, so theoretically, ALL of those additional events could be triggered simultaneously.

Thanks.
 
I don't do a whole lot of Python work, so the syntax here is going to be off but it will essentially look like this :

SomeVariable = getSorenRandNum(100)
If SomeVariable < 33 then
// code for possibility 1
else if SomeVariable <67
// code for possibility 2
 
Right. So I suppose it would go something like this:

Code:
def applyEVENT1(argsList):

   pPlayer = gc.getPlayer(kTriggeredData.ePlayer)
   SomeVariable = getSorenRandNum(100)
   tEvent1 = CvUtil.findInfoTypeNum(gc.getEventTriggerInfo, gc.getNumEventTriggerInfos(),'EVENTTRIGGER_EVENT_1')
   tEvent2 = CvUtil.findInfoTypeNum(gc.getEventTriggerInfo, gc.getNumEventTriggerInfos(),'EVENTTRIGGER_EVENT_2')

f SomeVariable < 33 then:

    pPlayer.trigger(tEvent1)

elif SomeVariable < 67 then:
    
    pPlayer.trigger(tEvent2)

elif SomeVariable < 100 then:

   return False

But isn't there a danger that if, for instance, SomeVariable = 20 then both tEvent1 and tEvent2 would get triggered? Or does the if/then statement preclude this?
 
This is what you should be aiming for:

Code:
     def doEventTrigger(self):
               # Get random num 0 - 99
               iRandNum = (gc.getGame().getSorenRandNum(100, "D100 Roll") + 1)
               if (iRandNum > 67):
                    # Do nothing
                    return
               if (iRandNum < 34):
                    # Trigger Event A
               else:
                    #Trigger Event B
               return

The if statements firstly get rid of the "do nothing" segment of the dice roll. Then since it's "one or the other" it's a simple if .. else .. statement.
 
Yes, but it's stupidly high :)

65535 I believe, could be wrong though since i never really tried anything higher.
 
I have no idea when a 1 in a million shot would be needed. Seems pointless in a game of a thousand or two turns. :)

And the odds you wanted for your events could easily have been 1 in 3 instead of 33 in 100 as you have it now. ;)
 
Hey, please forgive the question if the answer is blindingly obvious...but if I use:

Code:
 iRandNum = (gc.getGame().getSorenRandNum(100, "D100 Roll") + 1)

...will the game potentially generate a "0" as one of the random numbers? I am assuming this is why Dale added the "+1" at the end. So if I actually want to use an unmodified result to provide 1:100 odds, I would have to generate a random number up to 99:

Code:
(gc.getGame().getSorenRandNum(99, "D100 Roll")

Reason I'm asking is because I want to provide a binary either/or random choice -- if 1: do x, if 2: do z. But if zero will be generated, then I should set it up as if 0: do x, if 1: do z.

Thanks.
 
Yes, python is like most languages where if you ask for random(X) you get 0>y>X, but not inclusive of X.

So for random(2) you would check for 0 or 1.
 
Back
Top Bottom