-4
\$\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\$

closed as off-topic by isaacg, haykam, Leaky Nun, R. Kap, NoOneIsHere Jul 30 '16 at 12:05

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\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\$ – El'endia Starman Jul 30 '16 at 1:08
  • 2
    \$\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 Jul 30 '16 at 1:09
  • 2
    \$\begingroup\$ What characters will the string contain? Is it alright if the probability is not exactly uniform? \$\endgroup\$ – Leaky Nun Jul 30 '16 at 1:14
  • \$\begingroup\$ lol 6 ansers from one user! \$\endgroup\$ – Rohan Jhunjhunwala Jul 30 '16 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 Jul 30 '16 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\$
  • \$\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\$ – haykam Jul 30 '16 at 1:14
  • \$\begingroup\$ @Peanut It works perfectly. \$\endgroup\$ – R. Kap Jul 30 '16 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 Jul 30 '16 at 2:55
  • \$\begingroup\$ @isaacg, I was using iOS 10 developer beta 1, and Safari 10. \$\endgroup\$ – haykam Jul 30 '16 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\$
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
\$\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
\$\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
\$\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.