randomly selecting multiple items from a list help please

Freeform discussion about anything related to modding Transcendence.
Post Reply
Militia Captain
Militia Captain
Posts: 624
Joined: Tue Nov 05, 2013 9:56 am

Thu Jan 10, 2019 11:11 pm

Although it isn't a very complicated task, is there is an easy way to select two or more items randomly from a list?

Wanting to damage between 2 to 4 of the 5 installed weapons on the CSC-II Earth in the Network mod to simulate battle damage. So I think a list of 2 to 4 weapons selected randomly is needed so the weapon property can be set to 'damaged'.

Using 'random' on the list of the CSC's installed weapons would work for one weapon but using it more than once has a chance of selecting the same weapon again.

Is there a simple way of selecting 2-4 of 5 items in a list without having to use code to check if an item is already selected? All I can think of is using for/enum/lnkAppend or similar with a check that the item hasn't already been appended but this seems a bit longwinded.
Or is there another way to only damage 2-4 of the weapons? TIA.

Militia Lieutenant
Militia Lieutenant
Posts: 147
Joined: Sat Jan 21, 2017 2:53 am

Fri Jan 11, 2019 2:17 am

Generate a random number in [0..4]. That's your first index. Add 1d4 and mod 5 it; that's your second index. Damage those two devices.

(This only works for damaging exactly 2 devices though. If you want to do more, recommend you shuffle [0, 1, 2, 3, 4], then pick a random number of indices off the front of the shuffled list)

Militia Commander
Militia Commander
Posts: 485
Joined: Tue Mar 05, 2013 8:26 am

Fri Jan 11, 2019 7:56 am

(subset (shuffle itemList) 0 n)

Post Reply