# Constructed List with Prohibitions

I'm trying to create a constructed list that shows 5 random items from a list of 60 items. However, some items can't show up together such as, 1 & 2, or 3, 4, & 5, or 6 & 7, etc. (essentially prohibitions). Do you know the best way to program this so I end up with a list of 5 items that are truly random?

Interesting question.  I'm not a stats expert, but unless there are certain parallels in your prohibitions, I'm not certain that there is a perfectly fair solution to this problem.  The frequency that the prohibited items appear cannot be the same as the frequency of the non-prohibited items.

A "close enough" solution may be to go through picking items at random, but simply skipping past any items that are not valid with previously selected items.  Here's constructed list instructions that should do just that:

```Begin Unverified Perl
# Params
my \$items = 60;
my \$choose = 5;
my %prohibitions = (
1 => ,
2 => ,
3 => [4, 5],
4 => [3, 5],
5 => [3, 4],
6 => ,
7 => 
);

# Run
my @arr = (1..\$items);
my \$i = \$items;
while (\$i--) {
my \$j = int(rand(\$i + 1));
next if \$i == \$j;
@arr[\$i, \$j] = @arr[\$j, \$i];
}

my \$selections = 0;
my %banned = ();
while (\$selections < \$choose) {
my \$item = shift(@arr);
if (!%banned{\$item}) {
my \$prohs = \$prohibitions{\$item};
if (\$prohs) {
foreach my \$proh (@{\$prohs}) {
\$banned{\$proh} = 1;
}
}
\$selections++;
}
}
End Unverified
```
answered Aug 17, 2021 by Platinum (201,875 points)
This worked perfectly! Thank you for helping & providing this code!
Error Using Actual Labels
I actually have a follow up question on this. I was testing this and only had numbers as the labels and everything worked perfectly. But once I added the actual labels (they're long sentences) it was giving me error #132. In my error log it says:
System Error:
syntax error at (eval 225) line 28, near "%banned{" syntax error at (eval 225) line 37, near "; }"

When I test locally it works fine, but doesn't work when I upload to our server.
Did you also change any part of the constructed list code when you changed your labels?
The only thing I changed was we have more prohibitions so I added those in but didn't change anything else. This is what my code looks like:
```Begin Unverified Perl
# Params
my \$items = 60;
my \$choose = 5;
my %prohibitions = (
1 => ,
2 => ,
3 => [4, 5],
4 => [3, 5],
5 => [3, 4],
6 => ,
7 => ,
8 => ,
9 => ,
11 => [12, 13],
12 => [11, 13],
13 => [11, 12],
14 => ,
15 => ,
16 => [17, 18],
17 => [16, 18],
18 => [16, 17],
20 => [21, 22],
21 => [20, 22],
22 => [20, 21],
23 => ,
24 => ,
25 => [26, 27],
26 => [25, 27],
27 => [25, 26],
30 => ,
31 => ,
34 => ,
35 => ,
37 => ,
38 => ,
39 => [40, 41],
40 => [39, 41],
41 => [39, 40],
42 => [43, 44],
43 => [42, 44],
44 => [42, 43],
45 => ,
46 => ,
47 => ,
48 => ,
50 => ,
51 => ,
54 => ,
55 => ,
56 => [57, 58],
57 => [56, 58],
58 => [56, 57],
59 => ,
60 => 
);

# Run
my @arr = (1..\$items);
my \$i = \$items;
while (\$i--) {
my \$j = int(rand(\$i + 1));
next if \$i == \$j;
@arr[\$i, \$j] = @arr[\$j, \$i];
}

my \$selections = 0;
my %banned = ();
while (\$selections < \$choose) {
my \$item = shift(@arr);
if (!%banned{\$item}) {
my \$prohs = \$prohibitions{\$item};
if (\$prohs) {
foreach my \$proh (@{\$prohs}) {
\$banned{\$proh} = 1;
}
}