# Using RandomNum not working? Returning same value

## Recommended Posts

Hi,

I had seen a script done by @DavidW using RandomNum. In his example there are 9 different creatures each tied to a different number from 1-9 and depending on which number is called I would assume then the corresponding creature would spawn. Using his example I created my own version from a small pool, 1-5, however for some reason the creature which I have set to be the fifth and final number is always the one which ends up being spawned. I don't really understand why the script doesn't actually do the random number check. I have done multiple new games and reloads to test the script and it keeps returning the same creature each time.

Attached is a copy of the .BCS I used, which is tied to the creatures themselves.

Thank you.

You have it backwards. It should be RandomNum(5,1), (5,2), (5,3), (5/4), and (5/5)

21 minutes ago, morpheus562 said:

You have it backwards. It should be RandomNum(5,1), (5,2), (5,3), (5/4), and (5/5)

Thank you so much, it works fine now. I didn't even think to reverse the numbers. Why does it work with (5,1) and not with (1.5) if you don't mind me asking?

25 minutes ago, Moonboy187 said:

Why does it work with (5,1) and not with (1.5) if you don't mind me asking?

Because (5,1) tells the engine that it's one out of 5. (1,5) I'm not sure makes even sense to the engine - 5 out of 1? From what you experienced, it seems to translated to "always".

A way to think about it is that (5,1) means „there are 5 random outcomes of equal likelihood; this is the value returned for the first of them”.

(1,5) would similarly mean „there is 1 outcome of equal likelihood, this is the value returned for the fifth of them”, and the fifth roll of a 1-sided dice will always return a 1.

6 minutes ago, jastey said:

Because (5,1) tells the engine that it's one out of 5. (1,5) I'm not sure makes even sense to the engine - 5 out of 1? From what you experienced, it seems to translated to "always".

I see, when I looked at it at first I was under the assumption that (1,5) meant that there is a 1 in five chance of being x, it never occurred to me that the opposite is actually the right way.

3 minutes ago, Almateria said:

A way to think about it is that (5,1) means „there are 5 random outcomes of equal likelihood; this is the value returned for the first of them”.

(1,5) would similarly mean „there is 1 outcome of equal likelihood, this is the value returned for the fifth of them”, and the fifth roll of a 1-sided dice will always return a 1.

Thank you both for the explanation, this game engine is quite puzzling at times.

More accurately, here's the relevant entry for this script trigger from the IESDP:

Quote
Generates a random number between `1` and `Range`. Returns true only if the random number equals the 2nd parameter.
Note: Scripting uses the same random value to seed all `RandomNum()` triggers across a single tick; if you have multiple `RandomNum()` calls with the same parameters, they will all generate the same result.

The first parameter is the range of possible values for the random number, while the second parameter is the value to test against. Roll the dice, then test them. And because the seed is the same for "simultaneous" calls of this, you can use several instances to implement a random switch in a DLG or BCS. More useful for the former, as the "RESPONSE #" values can be used for random switching in a BCS. An example (from BPPORTL.BCS, spawning random monsters from portals):

Spoiler

IF
Global("OOZE_SPAWN","LOCALS",1)
THEN
RESPONSE #30
CreateCreatureObjectOffset("BPJLGR01",Myself,[1.1])  // Gray Ooze
SetGlobal("OOZE_SPAWN","LOCALS",0)
RESPONSE #20
CreateCreatureObjectOffset("BPJLMU01",Myself,[1.1])  // Mustard Jelly
SetGlobal("OOZE_SPAWN","LOCALS",0)
RESPONSE #20
CreateCreatureObjectOffset("BPJLOC01",Myself,[1.1])  // Ochre Jelly
SetGlobal("OOZE_SPAWN","LOCALS",0)
RESPONSE #10
CreateCreatureObjectOffset("BPSLFS01",Myself,[1.1])  // Fission Slime
SetGlobal("OOZE_SPAWN","LOCALS",0)
END

Four possible spawns, in a 30/20/20/10 ratio.

## Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.