randomly selecting multiple items from a list help please

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

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.
Stupid code. Do what I want, not what I typed in!
Derakon
Militia Lieutenant
Militia Lieutenant
Posts: 175
Joined: Sat Jan 21, 2017 2:53 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)
NMS
Militia Captain
Militia Captain
Posts: 569
Joined: Tue Mar 05, 2013 8:26 am

(subset (shuffle itemList) 0 n)
relanat
Militia Captain
Militia Captain
Posts: 941
Joined: Tue Nov 05, 2013 9:56 am

Awesome. Thanks. I figured there had to be an easy way.

What does 'mod' do in the first example? The two examples in the source code don't help much. And does it matter if it is (mod 5 n) or (mod n 5)? I think that's what is meant.
Stupid code. Do what I want, not what I typed in!
Derakon
Militia Lieutenant
Militia Lieutenant
Posts: 175
Joined: Sat Jan 21, 2017 2:53 am

relanat wrote:
Tue Jan 22, 2019 12:47 am
Awesome. Thanks. I figured there had to be an easy way.

What does 'mod' do in the first example? The two examples in the source code don't help much. And does it matter if it is (mod 5 n) or (mod n 5)? I think that's what is meant.
Mod is also known as % or the remainder operation. X % Y is "whatever is left over after dividing X by Y". So for example, 12 % 5 is 2, 8 % 5 is 3, and 16 % 4 is 0. The nice thing about % is that it readily gives you a number from 0 up to your divisor minus 1.
relanat
Militia Captain
Militia Captain
Posts: 941
Joined: Tue Nov 05, 2013 9:56 am

Thank you. This page is going in the helpful info folder.
Stupid code. Do what I want, not what I typed in!
Post Reply