# Random character from a string [closed]

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.

### Examples

• xyz: x, y, or z
• xxX: x, x, or X
• }^€: }, ^, or €
• xxx: x
• 0123456789: random number, 0-9

## closed as off-topic by isaacg, haykam, Leaky Nun, R. Kap, NoOneIsHereJul 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.

• 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. – El'endia Starman Jul 30 '16 at 1:08
• 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. – Dennis Jul 30 '16 at 1:09
• What characters will the string contain? Is it alright if the probability is not exactly uniform? – Leaky Nun Jul 30 '16 at 1:14
• lol 6 ansers from one user! – Rohan Jhunjhunwala Jul 30 '16 at 1:47
• 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. – isaacg Jul 30 '16 at 3:04

# Pyth, 1 byte

O


Try it online!

### Explanation

From the definition of the operator O:

op. arg.    func.
O   <col>   Random element of A.

• The Pyth executer doesn't work on mobile. I will try it when I can use a computer; can someone else verify this works? – haykam Jul 30 '16 at 1:14
• @Peanut It works perfectly. – R. Kap Jul 30 '16 at 1:16
• @Peanut It works fine on mine mobile, what OS/Browser are/were you using? I'd like to make it more general. – isaacg Jul 30 '16 at 2:55
• @isaacg, I was using iOS 10 developer beta 1, and Safari 10. – haykam Jul 30 '16 at 2:57

# 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.

# CJam, 3 bytes

lmR


Try it online!

lmR
mR  random element


# 05AB1E, 2 bytes

.R


from Info.txt:

.R      = pop a      push random_pick(a)


Try it online!

# 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.

# 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"))

# 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.

# 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>)).

# Java 8, 44 bytes

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


Try it online!

# Javascript, 40 bytes

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


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

# 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;


# q (4)

Will return a random item from a list:

rand