Skip to main content
Commonmark migration
Source Link

#Java 8, 127 122 121 bytes

Java 8, 127 122 121 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;Collections.shuffle(l))l.add(i);return l.get(0);}

-1 byte thanks to @Nevay.

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
        Collections.shuffle(l))
                           //   and shuffle the List randomly after every iteration
      l.add(i);            //    Add `i` that many times to List `l`
                           //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
  return l.get(0);         //  And then return the first item of the list
}                          // End of method

#Java 8, 127 122 121 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;Collections.shuffle(l))l.add(i);return l.get(0);}

-1 byte thanks to @Nevay.

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
        Collections.shuffle(l))
                           //   and shuffle the List randomly after every iteration
      l.add(i);            //    Add `i` that many times to List `l`
                           //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
  return l.get(0);         //  And then return the first item of the list
}                          // End of method

Java 8, 127 122 121 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;Collections.shuffle(l))l.add(i);return l.get(0);}

-1 byte thanks to @Nevay.

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
        Collections.shuffle(l))
                           //   and shuffle the List randomly after every iteration
      l.add(i);            //    Add `i` that many times to List `l`
                           //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
  return l.get(0);         //  And then return the first item of the list
}                          // End of method
added 129 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 8, 127 122122 121 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;l>0;Collections.addshuffle(il));Collectionsl.shuffleadd(li);return l.get(0);}

-1 byte thanks to @Nevay.

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
      l  Collections.addshuffle(il))
                           //   and Andshuffle the List randomly after every iteration
      l.add(i);            //    Add `i` that many times to List `l`
    );                       //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
  Collections.shuffle(l);  //  Shuffle the List randomly
  return l.get(0);         //  And then return the first item of the list
}                          // End of method

#Java 8, 127 122 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;l.add(i));Collections.shuffle(l);return l.get(0);}

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
      l.add(i)             //    And add `i` that many times to List `l`
    );                     //   End of inner loop (2)
                           //  End of loop (1) (implicit / single-line body)
  Collections.shuffle(l);  //  Shuffle the List randomly
  return l.get(0);         //  And then return the first item of the list
}                          // End of method

#Java 8, 127 122 121 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;Collections.shuffle(l))l.add(i);return l.get(0);}

-1 byte thanks to @Nevay.

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
        Collections.shuffle(l))
                           //   and shuffle the List randomly after every iteration
      l.add(i);            //    Add `i` that many times to List `l`
                           //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
  return l.get(0);         //  And then return the first item of the list
}                          // End of method
added 93 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 8, 127127 122 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;l.add(i));Collections.shuffle(l);return l.get((int)(Math.random()*l.size())0);}

Uses the samea similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.Try it here.

import java.util.*;        // Required import for List, Stack and StackCollections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
      l.add(i)             //    And add `i` that many times to List `l`
    );                     //   End of inner loop (2)
                           //  End of loop (1) (implicit / single-line body)
  return l.get((int)(Math.random()*lCollections.sizeshuffle())l);
       //  Shuffle the List randomly
  return l.get(0);         //  ReturnAnd athen randomreturn the first item of the Listlist
}                          // End of method

#Java 8, 127 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;l.add(i));return l.get((int)(Math.random()*l.size()));}

Uses the same approach as @ErikTheOutgolfer's Jelly answer.

Explanation:

Try it here.

import java.util.*;     // Required import for List and Stack
a->{                    // Method with integer-array parameter and integer return-type
  List l=new Stack();   //  Create a List
  for(int i:a)          //  Loop (1) over the input array
    for(int j=i;j-->0;  //   Inner loop (2) from `i` down to 0
      l.add(i)          //    And add `i` that many times to List `l`
    );                  //   End of inner loop (2)
                        //  End of loop (1) (implicit / single-line body)
  return l.get((int)(Math.random()*l.size()));
                        //  Return a random item of the List
}                       // End of method

#Java 8, 127 122 bytes

import java.util.*;a->{List l=new Stack();for(int i:a)for(int j=i;j-->0;l.add(i));Collections.shuffle(l);return l.get(0);}

Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding n times the item n to the list, and then select one randomly from that list.

Explanation:

Try it here.

import java.util.*;        // Required import for List, Stack and Collections
a->{                       // Method with integer-array parameter and integer return-type
  List l=new Stack();      //  Create a List
  for(int i:a)             //  Loop (1) over the input array
    for(int j=i;j-->0;     //   Inner loop (2) from `i` down to 0
      l.add(i)             //    And add `i` that many times to List `l`
    );                     //   End of inner loop (2)
                           //  End of loop (1) (implicit / single-line body)
  Collections.shuffle(l);  //  Shuffle the List randomly
  return l.get(0);         //  And then return the first item of the list
}                          // End of method
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading