Randomness problem

I was playing around a bit with a seemingly simple set of instructions, and I ran into an issue I can’t wrap my head around.
First, here’s the script simplified and reconstructed for the purpose of this post (the result is the same):

Example Location is a room. 

A bag is a kind of thing.
A treasure bag is a kind of bag.
An empty bag is a kind of bag.
There are 3 empty bags.
There is 1 treasure bag.

Man 1, Man 2 and Man 3 are people.

When play begins:
	repeat with N running through people:
		now N is in the location of the player;
		now N is carrying a random bag.

This compiles nicely, but one or more of the characters (player included) almost always end up with no bag at all, even though there clearly is a bag for everyone. It takes around ten tries for them to get one each. Why does this happen? Does “a random bag” include no bag at all, or does “repeat with N” not work the way I think it does?

I can see when the player gets a bag. How do you check if they get a bag?

After the first one, any “random bag” may well be one of the ones which has already been allocated. That’s how you end up with several bags still off-stage, and several people with no bag (having been given one and then had it whisked away again).

Try this:

When play begins:
	repeat with N running through people:
		if N is off-stage:
                       now N is in the location;
		move a random off-stage bag to N.

GiannisG: the easiest way is with the SHOWME testing command.

Ohh right! Now I feel dumb. In my head a bag couldn’t be allocated twice, but of course it can.
A random off-stage bag makes sense! It’s the small details that give you a headache. :smiley: Thanks!

GiannisG: As jrb said, typing “showme Man 1” lists everything about Man 1, including his inventory.

Haha, I translated your code to work with hats instead of bags, thinking that I would at least see the hats worn by the others, but I guess that doesn’t work either. Thanks for the showme tip.

By the way, wouldn’t it be simpler to have the bag defined as a new kind of container and then have four identical bags carried by the people? Then, you could simply arrange for the treasure (a thing) to be in a random bag.

Of course, I don’t know the bigger picture of your game, so maybe this doesn’t work for you.