-5
\$\begingroup\$

Your goal in this is to pick a random character from a string. If a character appears X times, and there are Y characters, then the chance of that character being picked will be X/Y.

Input

The program takes a string as an input.

Output

Your program outputs a character.

Examples

  • xyz: x, y, or z
  • xxX: x, x, or X
  • }^€: }, ^, or €
  • xxx: x
  • 0123456789: random number, 0-9
\$\endgroup\$
5
  • \$\begingroup\$ In the future, please use the Sandbox for your challenges. If one user posts four very short answers in quick succession, that's a good sign your challenge is trivial. \$\endgroup\$ Commented Jul 30, 2016 at 1:08
  • 3
    \$\begingroup\$ Posting the same comment on every single answer is really unnecessary, especially if the answers are 1 byte long and thus must consist of a random-choice built-in. \$\endgroup\$
    – Dennis
    Commented Jul 30, 2016 at 1:09
  • 2
    \$\begingroup\$ What characters will the string contain? Is it alright if the probability is not exactly uniform? \$\endgroup\$
    – Leaky Nun
    Commented Jul 30, 2016 at 1:14
  • \$\begingroup\$ lol 6 ansers from one user! \$\endgroup\$ Commented Jul 30, 2016 at 1:47
  • 4
    \$\begingroup\$ I'm voting to close this question as off-topic because: Too Narrow There are either too few possible ways to answer, or answers would only differ in the language used. \$\endgroup\$
    – isaacg
    Commented Jul 30, 2016 at 3:04

12 Answers 12

2
\$\begingroup\$

Pyth, 1 byte

O

Try it online!

Explanation

From the definition of the operator O:

op. arg.    func.
O   <col>   Random element of A.
\$\endgroup\$
4
  • \$\begingroup\$ The Pyth executer doesn't work on mobile. I will try it when I can use a computer; can someone else verify this works? \$\endgroup\$ Commented Jul 30, 2016 at 1:14
  • \$\begingroup\$ @Peanut It works perfectly. \$\endgroup\$
    – R. Kap
    Commented Jul 30, 2016 at 1:16
  • \$\begingroup\$ @Peanut It works fine on mine mobile, what OS/Browser are/were you using? I'd like to make it more general. \$\endgroup\$
    – isaacg
    Commented Jul 30, 2016 at 2:55
  • \$\begingroup\$ @isaacg, I was using iOS 10 developer beta 1, and Safari 10. \$\endgroup\$ Commented Jul 30, 2016 at 2:57
1
\$\begingroup\$

Jelly, 1 byte

X

Try it online!

Explanation

From the definition of the operator X:

Arity: 1

Name: Random

Function: choose a random item from z if z is a list, or from 1 to z inclusive if z is a positive integer. If z = 0, return z. Error if z is negative or a decimal.

\$\endgroup\$
0
1
\$\begingroup\$

CJam, 3 bytes

lmR

Try it online!

lmR
l    read line
 mR  random element
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 2 bytes

.R

from Info.txt:

.R      = pop a      push random_pick(a)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C, 30 bytes

This answer must run on a 32-bit platform and requires the GNU C library implementation.

f(s){return*(char*)strfry(s);}

The strfry function shuffles the characters in a string. We first shuffle the input string, then return the first character.

\$\endgroup\$
0
\$\begingroup\$

Actually, 1 byte

J

Try it online!

Explanation

From the definition of the operator J:

4A (J): pop a: push a random integer in [0,a) (randrange(a)); pop [a] or "a": push a random element from [a] or "a" (random.choice([a]|"a"))

\$\endgroup\$
0
0
\$\begingroup\$

J, 5 bytes

?@#{]

Usage

>> f =: ?@#{]
>> f 'xyz'
<< x

Explanation

This is a fork, where f is ?@#, g is {, and h is ].

Therefore, (?@#{])'xyz', when expanded, would become ((?@#)'xyz') ({) ((])'xyz')

The (?@#) is just a composed function, so (?@#)'xyz' expanded would become ?(#'xyz').

#'xyz' gets the length of 'xyz', then ? generates a random non-negative integer below the length.

The { then takes the item at the corresponding index.

\$\endgroup\$
0
0
\$\begingroup\$

Python 3.5 and Python 2.7, 26 bytes:

from random import*;choice

A function declaration. Name the function, and then call it like print(<Function Name>(<String>)).

\$\endgroup\$
0
0
\$\begingroup\$

Java 8, 44 bytes

x->x.charAt((int)(Math.random()*x.length()))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Javascript, 40 bytes

x=>x[Math.floor(x.length*Math.random())]

f=x=>x[Math.floor(x.length*Math.random())]
alert(f(prompt()))

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 11 bytes

*.comb.pick
*.comb.roll

Explanation:

  • * starts the Whatever lambda
  • .comb splits the string into a list of grapheme clusters
  • .roll/.pick grabs a single element from the list at random
    ( so it is effectively a weighted roll )

( There isn't a difference between .pick and .roll unless you give them an argument that is something other than 1 )

Usage:

say ( *.comb.pick )( 'xxX' ); # ⅔ chance of 「x」, ⅓ chance of 「X」

my &code = *.comb.pick;

say code 'xyz'; # ⅓ chance of each of 「x」 「y」 or 「z」

say ('}^€:','0123456789').map: *.comb.pick;
\$\endgroup\$
0
\$\begingroup\$

q (4)

Will return a random item from a list:

rand
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.