# Sending the user to a random room based on a few odds?

Quick question, let’s say I have these three variables:
chanceToGoRoom1 = 56
chanceToGoRoom2 = 4
chanceToGoRoom3 = 40

I want to send the player to a random room when they press the [[next room]] based on these chances taking 100 as the max amount.

You didn’t state if the three variables being declare were Story or Temporary scoped, so I will assume they are temporary and that an `(set:)` macro call like the following is being done earlier in the Passage that contains the link.

``````(set: _chanceToGoRoom1 to 56, _chanceToGoRoom2 to 4, _chanceToGoRoom3 to 40)
``````

The `(random:)` macro can be used to inclusively generate a random integer within a range. In this specific case that range would be between 1 and the sum of all the related ‘chance’ variables.

``````(set: _roll to (random: 1, _chanceToGoRoom1 + _chanceToGoRoom2 + _chanceToGoRoom3))
``````

The `(cond:)` macro can be used to determine which Passage to transition to based on the value of the roll.

``````(set: _passage to (cond: _roll <= _chanceToGoRoom1, 'Room1', _roll <= (_chanceToGoRoom1 + _chanceToGoRoom2), 'Room2', 'Room3'))
``````

note: The logic of above works like so:

1. the range of roll is between 1 and 100.
2. a roll between 1 and 56 (inclusive) results in the ‘Room1’ Passage being selected.
3. a roll between 57 and 60 (inclusive) results in the ‘Room2’ Passage being selected.
4. a roll between 61 and 100 (inclusive) results in the ‘Room3’ Passage being selected.

At this point there are basically two potential techniques that can be used to combine the above together to cause a Passage Transition:

1: Pre-determine the Passage before showing the link.

This allows a `(link-goto:)` macro to be uses to create the link, but it does allow a knowledgeable end-user to determine and potentially change which Passage they will be sent to before selecting the link.

``````(set: _roll to (random: 1, _chanceToGoRoom1 + _chanceToGoRoom2 + _chanceToGoRoom3))

(set: _passage to (cond: _roll <= _chanceToGoRoom1, 'Room1', _roll <= (_chanceToGoRoom1 + _chanceToGoRoom2), 'Room2', 'Room3'))

``````

2: Determine the Passage after the end-user selects the link.

In this case a `(link:)` plus `(go-to:)` macro combination will be used to cause the transition, and because the choice of target Passage is done within the link’s Hook the end-user can’t determine or alter which Passage.

``````(link: 'next room')[
(set: _roll to (random: 1, _chanceToGoRoom1 + _chanceToGoRoom2 + _chanceToGoRoom3))
(set: _passage to (cond: _roll <= _chanceToGoRoom1, 'Room1', _roll <= (_chanceToGoRoom1 + _chanceToGoRoom2), 'Room2', 'Room3'))
(go-to: _passage)
]
``````

My bad, they were supposed to be Story scoped. As like, your choices change your chances of going to a different room doesn’t matter when you made them choices. think of it like if you said a person bad thing, they have a chance to backstab you in the future.

I’ll probably go with the first (before) technique since it makes more sense to me but the inclusion of the second technique is well appreciated!