title: R: Randomly Selecting Items from a List
body: <p>I am working with the R programming language. I want to create a problem in which:</p>
  <ul>
  <li><p><strong>There are 4 people</strong> : <code>&quot;person 1&quot;, &quot;person 2&quot;, &quot;person 3&quot; ,&quot;person 4&quot;</code></p>
  </li>
  <li><p><strong>There is a list of food items</strong>: <code>&quot;pizza&quot;, &quot;apples&quot;, &quot;tacos&quot;, &quot;ice cream&quot;, &quot;grapes&quot;, &quot;olives&quot;, &quot;sushi&quot;, &quot;chocolate&quot;, &quot;cake&quot;, &quot;nachos&quot;, &quot;pasta&quot;, &quot;cookies&quot;, &quot;popcorn&quot;, &quot;soup&quot;</code></p>
  </li>
  <li><p><strong>There is a list of days in the year</strong>: <code>1 to 365</code></p>
  </li>
  </ul>
  <p>In this problem, the list of people is randomly sorted. The order of this list decides who get to &quot;pick first&quot;.</p>
  <p>According to the order of the list, the person at the start of the list will be assigned 4 random numbers:</p>
  <ul>
  <li><p><strong>Rand_1_1</strong> (Person 1, First Random Number) : The first random number will decide the number of &quot;food items&quot; the first person is allowed to pick (e.g. 3)</p>
  </li>
  <li><p><strong>Rand_1_2</strong> (Person 1, Second Random Number) : The second random number will decide which &quot;food items&quot; the first person will pick with the number of &quot;food items&quot; corresponding to &quot;Rand_1_1&quot; (e.g. &quot;tacos&quot;, &quot;nachos&quot;, &quot;soup&quot;)</p>
  </li>
  <li><p><strong>Rand_1_3</strong> (Person 1, Third Random Number): The third random number will decide the one of the bound for the days for the first person</p>
  </li>
  <li><p><strong>Rand_1_4</strong> (Person 1, Fourth Random Number): The fourth random number will decide the other bound for the days for the first person (e.g. Person 1 might be assigned days &quot;41 to 160&quot;).</p>
  </li>
  </ul>
  <p>This is random number assignment process is then completed for all people : <strong>Rand_1_1, Rand_1_2, Rand_1_3, Rand_1_4 , Rand_2_1, Rand_2_2, Rand_2_3, Rand_2_4, Rand_3_1, Rand_3_2, Rand_3_3, Rand_3_4, Rand_4_1, Rand_4_2, Rand_4_3, Rand_4_4</strong></p>
  <p>However, there is one <strong>logical constraint</strong>:</p>
  <ul>
  <li><p>A &quot;person&quot; can not choose &quot;food items&quot; that have already been chosen by the &quot;person&quot; before them (e.g. if Person 1 was selected to choose before Person 2, and if Person 1 chose &quot;pizza&quot; then Person 2 can not choose &quot;pizza&quot;)</p>
  </li>
  <li><p>A &quot;person&quot; can not choose a &quot;range of days&quot; that has already been chosen by the &quot;person&quot; before them</p>
  </li>
  <li><p>After all 4 people have finished choosing, <strong>it is possible</strong> that some &quot;food items&quot; and some &quot;date ranges&quot; can <strong>remain unselected</strong>. On the other hand, suppose if Person 1 chooses first and he happens to select all the &quot;food items&quot; - then of course, the other people will have no &quot;food items&quot;. The same logic applies for &quot;date ranges&quot;.</p>
  </li>
  </ul>
  <p><strong>Now, I am trying to code this in R:</strong></p>
  <p><strong>First</strong>, I loaded the libraries:</p>
  <pre><code>#load libraries
  
  library(dplyr)
  library(gtools)
  </code></pre>
  <p><strong>Second</strong>, I created the data:</p>
  <pre><code>#create data
  
  #variable 1: days of the year 
  
  days &lt;- 1:365
  
  #variable 2 : food
  
  food &lt;- c(&quot;pizza&quot;, &quot;apples&quot;, &quot;tacos&quot;, &quot;ice cream&quot;, &quot;grapes&quot;, &quot;olives&quot;, &quot;sushi&quot;, &quot;chocolate&quot;, &quot;cake&quot;, &quot;nachos&quot;, &quot;pasta&quot;, &quot;cookies&quot;, &quot;popcorn&quot;, &quot;soup&quot;)
  food_data &lt;- data.frame(food)
  food_data$id &lt;-  1:nrow(food_data)
  
  # people 
  people &lt;- c(&quot;person 1&quot;, &quot;person 2&quot;, &quot;person 3&quot; ,&quot;person 4&quot;)
  </code></pre>
  <p><strong>Third</strong>, I created the order in which the people will pick:</p>
  <pre><code># randomize order of people : this decides the order of who picks &quot;days&quot; and &quot;food&quot;
  
  set.seed(123)
  
  order_of_people = permute(people)
  
  # in this example, &quot;person 3&quot; will pick first, &quot;person 4&quot; will pick second, &quot;person 1&quot; will pick third and &quot;person 2&quot; will pick last
  
  order_of_people
  [1] &quot;person 3&quot; &quot;person 4&quot; &quot;person 1&quot; &quot;person 2&quot;
  </code></pre>
  <p><strong>My Problem:</strong> I know how to assign every person a random number, but I do not know how to assign the random numbers such that the logical constraints are respected. For example:</p>
  <pre><code>#choices for person 3 (according to the random seed, person 3 will pick 5 food items)
  
  set.seed(120)
  
  dim = dim(food_data)
  
  #number of items selected by person 3
  
  Rand_3_1 &lt;- sample.int(dim[1], 1)
  
  #which food items selected by person 3 (corresponding to the food_id : &quot;3, 9, 6, 7, 4&quot;)
  set.seed(120)
  
  Rand_3_2 = c( sample.int(dim[1], 1), sample.int(dim[1], 1), sample.int(dim[1], 1),  sample.int(dim[1], 1), sample.int(dim[1], 1))
  
  #which days selected by person 3 (according to this random seed, &quot;65 to 87&quot;)
  
  set.seed(120)
  
  Rand_3_3 &lt;- sample.int(365, 1)
  Rand_3_4 &lt;- sample.int(365, 1)
  </code></pre>
  <p>Thus, I can create &quot;selection frames&quot; for each person:</p>
  <pre><code>#Person 1
  Rand_1_1 &lt;- sample.int(dim[1], 1)
  
  Rand_1_2 = c( #fill randomly with amount of items specified by Rand_1_1)
  
  Rand_1_3 &lt;- sample.int(365, 1)
  Rand_1_4 &lt;- sample.int(365, 1)
  
  #Person 2
  Rand_2_1 &lt;- sample.int(dim[1], 1)
  
  Rand_2_2 = c( #fill randomly with amount of items specified by Rand_2_1)
  
  Rand_2_3 &lt;- sample.int(365, 1)
  Rand_2_4 &lt;- sample.int(365, 1)
  
  
  #Person 3
  Rand_3_1 &lt;- sample.int(dim[1], 1)
  
  Rand_3_2 = c( #fill randomly with amount of items specified by Rand_3_1)
  
  Rand_3_3 &lt;- sample.int(365, 1)
  Rand_3_4 &lt;- sample.int(365, 1)
  
  #Person 4
  Rand_4_1 &lt;- sample.int(dim[1], 1)
  
  Rand_4_2 = c( #fill randomly with amount of items specified by Rand_4_1)
  
  Rand_4_3 &lt;- sample.int(365, 1)
  Rand_4_4 &lt;- sample.int(365, 1)
  </code></pre>
  <p>But I don't know how to do this such that the logical constraints are respected. In the end, I am trying to produce something like this:</p>
  <pre><code>#desired results
  Person 1 : &quot;apples, tacos&quot; and &quot;4-51&quot;
  Person 2: &quot;cookies, nachos, cake, olives&quot;, and &quot;56-180&quot;
  Person 3: &quot;ice cream&quot;, and &quot;200-214&quot;
  Person 4: &quot;sushi, popcorn&quot; and &quot;350-365&quot;
  </code></pre>
  <p>Can someone please show me how to do this?</p>
  <p>Thanks</p>