47
\$\begingroup\$

About the Series

This is a guest entry for the Random Golf of the Day series.

First off, you may treat this like any other code golf challenge, and answer it without worrying about the series at all. However, there is a leaderboard across all challenges. You can find the leaderboard along with some more information about the series in the first post.

Input

No input is taken.

Output

A single letter of the alphabet (case irrelevant), with an optional trailing newline. Each letter must have non-zero probability of being chosen, and all 26 probabilities must be distinct. To remove all ambiguity: Distinct means that there must not be two probabilities that are equal to each other.

Scoring

This is code golf. Shortest code in bytes wins.

A valid entry is a full program or function that has zero probability of not terminating.

Alphabet

To avoid confusion, the particular alphabet to be used is the Latin alphabet:

Either

ABCDEFGHIJKLMNOPQRSTUVWXYZ

or

abcdefghijklmnopqrstuvwxyz

You may choose to output upper case or lower case. Alternatively, you may choose to output different cases on different runs if that helps. The probability for a given letter is the probability of that letter appearing in either case (upper or lower).

Explanation

As it won't be at all obvious from the output, please include a clear explanation of how you achieved the 26 distinct probabilities.

Leaderboard

(from here)

var QUESTION_ID=89621,OVERRIDE_USER=20283;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

The first post of the series also generates an overall leaderboard.

To make sure that your answers show up, please start every answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

(The language is not currently shown, but the snippet does require and parse it, and I may add a by-language leaderboard in the future.)

\$\endgroup\$
  • \$\begingroup\$ how would you measure 26 distinct probabilities? by running program 26 times? \$\endgroup\$ – YOU Aug 12 '16 at 12:47
  • 1
    \$\begingroup\$ @YOU have a look through the solutions - there are a few different approaches with excellent explanations \$\endgroup\$ – trichoplax Aug 12 '16 at 12:49
  • \$\begingroup\$ If it is a function does it need to print or can it just return the character value? \$\endgroup\$ – Geoff Reedy Aug 13 '16 at 6:12
  • \$\begingroup\$ @Geoff According to our defaults for input and output, either printing to STDOUT or returning a character are fine. \$\endgroup\$ – trichoplax Aug 13 '16 at 10:48
  • \$\begingroup\$ @Geoff note that it must be a character, not just a number value representing it. For example, A rather than 65. \$\endgroup\$ – trichoplax Aug 13 '16 at 10:50

54 Answers 54

2
\$\begingroup\$

Mathematica, 34 bytes

RandomChoice[Range@26->Alphabet[]]

RandomChoice[wlist -> elist] gives a random choice of elist weighted by wlist.

\$\endgroup\$
  • \$\begingroup\$ Damn, beat me to it! \$\endgroup\$ – Greg Martin Aug 14 '16 at 9:21
2
\$\begingroup\$

Ruby, 42 bytes

$><<(?A..?Z).flat_map{|c|[c]*c.ord}.sample

Quite straightforward: Generate 65 A's; 66 B's; ... 90 Z's, and randomly pick one of the letters generated.

\$\endgroup\$
2
\$\begingroup\$

Julia, 24 bytes

!c='a':c|>rand;c()=!!'z'

Try it online!

How it works

The function c() simply calls ! twice, with initial argument z. In turn !c creates a character range from a to its argument c and pseudo-randomly selects a character from this range. The distribution of probabilities is as follows.

Let x1, …, x26 denote the letters of the alphabet in their natural order. Select a letter Y among these, uniformly at random, then select a letter X from L1, … Y, also uniformly at random.

Fix n and k in 1, …, 26.

If n ≤ k, then p(X = xn | Y = xk) = 1/k. On the other hand, if n > k, then p(X = xn | Y = xk) = 0.

Therefore, p(X = xn) = Σ p(Y = xk) * p(X = xn | Y = xk) = 1/26 · (1/n + ⋯ + 1/26), giving the following probability distribution.

p(X = a) = 103187226801/696049754400 ≈ 0.148247
p(X = b) =  76416082401/696049754400 ≈ 0.109785
p(X = c) =  63030510201/696049754400 ≈ 0.090555
p(X = d) =  54106795401/696049754400 ≈ 0.077734
p(X = e) =  47414009301/696049754400 ≈ 0.068119
p(X = f) =  42059780421/696049754400 ≈ 0.060426
p(X = g) =  37597923021/696049754400 ≈ 0.054016
p(X = h) =  33773473821/696049754400 ≈ 0.048522
p(X = i) =  30427080771/696049754400 ≈ 0.043714
p(X = j) =  27452509171/696049754400 ≈ 0.039440
p(X = k) =  24775394731/696049754400 ≈ 0.035594
p(X = l) =  22341654331/696049754400 ≈ 0.032098
p(X = m) =  20110725631/696049754400 ≈ 0.028893
p(X = n) =  18051406831/696049754400 ≈ 0.025934
p(X = o) =  16139182231/696049754400 ≈ 0.023187
p(X = p) =  14354439271/696049754400 ≈ 0.020623
p(X = q) =  12681242746/696049754400 ≈ 0.018219
p(X = r) =  11106469546/696049754400 ≈ 0.015956
p(X = s) =   9619183746/696049754400 ≈ 0.013820
p(X = t) =   8210176146/696049754400 ≈ 0.011795
p(X = u) =   6871618926/696049754400 ≈ 0.009872
p(X = v) =   5596802526/696049754400 ≈ 0.008041
p(X = w) =   4379932326/696049754400 ≈ 0.006293
p(X = x) =   3215969526/696049754400 ≈ 0.004620
p(X = y) =   2100505176/696049754400 ≈ 0.003018
p(X = z) =   1029659400/696049754400 ≈ 0.001479
\$\endgroup\$
2
\$\begingroup\$

LaTeX, 122 115 bytes

-7 bytes by replacing pgfmathparse{Hex(...)} with pgfmathHex{...}.

Or, if I'm allowed to skip the document class definition & setup, and just count the package import and functional code: 65 bytes.

pgfmathHex parses the expression and its (hexadecimal) result is then fed into \char which turns the code point into a unicode character. The expression itself is identical to many other answers here.

\documentclass{book}\usepackage{tikz}\begin{document}\pgfmathHex{65+sqrt(rnd)*26}\char"\pgfmathresult\char"\pgfmathresult\end{document}

Ungolfed (with cherry-picked for loop for page-filling output):

\documentclass{book}
\usepackage{tikz}
\begin{document}
\noindent
\foreach \n in {0,...,1513}
{
\pgfmathHex{65+sqrt(rnd)*26}\char"\pgfmathresult
}
\end{document} 

Output (w/ free page number :) ):

enter image description here

\$\endgroup\$
2
\$\begingroup\$

C#, 68 66 bytes

Thanks to Leaky Nun for fixing the chance distribution.

var r=new Random();char f(char c='A')=>c<'Z'&r.Next(3)>0?f(++c):c;

Surprisingly short for a C# answer.

It defines a function f that returns a random character between the given one and Z. It has a 1/3 chance of returning the current character, otherwise it recurses on the next. If the current character is Z, it always returns. Default parameter is A, so calling it without any argument satisfies the challenge.

The resulting chances follow 2^(n-1) / 3^n:

A: 1/3
B: 2/9
C: 4/27
D: 8/81
...
Y: 16777216/847288609443
Z: 67108864/2541865828329
\$\endgroup\$
  • 1
    \$\begingroup\$ Y and Z have the same probability. \$\endgroup\$ – Leaky Nun Aug 12 '16 at 14:35
  • 3
    \$\begingroup\$ You can use 3 instead of 2 to fix this problem. \$\endgroup\$ – Leaky Nun Aug 12 '16 at 14:35
  • \$\begingroup\$ Fixed the chances, thanks @LeakyNun \$\endgroup\$ – Scepheo Aug 15 '16 at 8:45
2
\$\begingroup\$

x86_64 machine language for Linux, 15 19 17 16 bytes

L1:
48 0f c7 f0             rdrand %rax
f3 48 0f b8 c0          popcnt %rax,%rax
3c 1a                   cmp    $0x1a,%al
7d f3                   jge    L1
04 41                   add    $0x41,%al
c3                      retq

This requires support for the POPCNT and RDRAND instructions.

A uniform distributed random number is generated, the number of 1's in that number is counted, if that number is less than 26, a letter is returned. One will need to let the code run a long time before one sees a letter A.

To test, try something like

#include<stdio.h>
#define TEST "\x48\xf\xc7\xf0\xf3\x48\xf\xb8\xc0\x3c\x1a\x7d\xf3\4\x41\xc3"
int main(){
  int hist[26]={0};
  for(int i=0;i<10000000;i++){
    hist[ ((int(*)())TEST)() - 'A' ]++;
  }
  for(int i=0;i<26;i++){
    printf("%c %d\n", 'A'+i, hist[i] );
  }
}

Sample output

A 0
B 0
C 0
D 0
E 0
F 0
G 0
H 0
I 0
J 0
K 0
L 8
M 32
N 137
O 511
P 1639
Q 5188
R 14475
S 37539
T 91670
U 205638
V 431381
W 842259
X 1536776
Y 2626524
Z 4206223

The analytical expression for the probability of each letter can be derived from the binomial distribution. The letter A is assigned index k=0, B is assigned k=1 and so on.

        /  \
       | 64 |
       | k  |
        \  /
p(k)=------------
      25
      --- /  \
      \  | 64 |
      /  | i  |
      --- \  /
      i=0

 p(A)~1.0483e-18
 p(B)~6.7093e-17
 p(C)~2.1134e-15
 p(D)~4.3678e-14
 p(E)~6.6608e-13
 p(F)~7.9930e-12
 p(G)~7.8598e-11
 p(H)~6.5124e-10
 p(I)~4.6401e-09
 p(J)~2.8872e-08
 p(K)~1.5879e-07
 p(L)~7.7953e-07
 p(M)~3.4429e-06
 p(N)~1.3772e-05
 p(O)~5.0169e-05
 p(P)~1.6723e-04
 p(Q)~5.1214e-04
 p(R)~1.4460e-03
 p(S)~3.7758e-03
 p(T)~9.1413e-03
 p(U)~2.0568e-02
 p(V)~4.3095e-02
 p(W)~8.4231e-02
 p(X)~1.5381e-01
 p(Y)~2.6276e-01
 p(Z)~4.2042e-01
\$\endgroup\$
  • \$\begingroup\$ Nice recovery :) \$\endgroup\$ – trichoplax Aug 14 '16 at 15:48
2
\$\begingroup\$

Matlab, 24 18 bytes

char(floor(26*rand^2+65))

it looks like using floor(x) isn't necessary as char also takes non-integer inputs

char(25*rand^2+65)

not using ['' x] instead of char(x) so I won't get warning: implicit conversion from numeric to char for "purer" output.

rand yields a uniformly distributed random number in the interval (0,1) but rand^2 isn't uniformy distributed anymore, the probability density function follows 1/x, see here.

with (b-a)*rand+a one can shift the interval of the distribution from (0,1) to (a,b), this also works with rand^2.

because i use floor i need to stretch the interval to (65,91) so i don't lose "Z"

It might no be the shortest answer but i like the approach with using a uniform random distribution to get a non-uniform random distribution.

Below is the count for each generated numbers after 20000 iterations (using my first version of my answer).

distribution after 20000 iterations

\$\endgroup\$
  • \$\begingroup\$ As long as it gives correct output, I don't see a reason to avoid warnings. Go ahead and save the byte... \$\endgroup\$ – trichoplax Aug 16 '16 at 22:59
  • \$\begingroup\$ But the warning gets also printed as output, does that still count as it produces more output that the desired character? \$\endgroup\$ – paul.oderso Aug 17 '16 at 4:57
  • \$\begingroup\$ Ah I see. If it's printed in conjunction with the output then that's a problem. I'd suggest checking if there's a command line switch that can suppress the warning at the cost of a byte but to save just one byte it's not worth it... \$\endgroup\$ – trichoplax Aug 17 '16 at 9:09
  • \$\begingroup\$ Consensus on meta seems to be that warnings to STDERR are fine but warnings to STDOUT are not . I'm not sure which Matlab uses, but mentioning in case it helps. \$\endgroup\$ – trichoplax Aug 17 '16 at 9:34
  • \$\begingroup\$ I'm not sure too, but thanks for the hint. Although running the code in Matlab which had warnings turned off with warning('off','all') beforehand suppresses the warning - but i have no clue if this complies with the rules? \$\endgroup\$ – paul.oderso Aug 17 '16 at 14:04
1
\$\begingroup\$

Clojure, 61 bytes

#(char(- 90(count(take-while(fn[a](<(rand)0.5))(range 26)))))

Takes each successive element from the range of 26 elements with probability 1/2. So the chance that one element is taken is 1/2, 2 elements - 1/4 and so on. After that subtract from 90 number of elements taken and turn it into char.

p(Z) = 1/2, p(Y) = 1/4, p(X) = 1/8 ... p(A) = 1/2^(26)

See it online: https://ideone.com/Cg15FY

\$\endgroup\$
  • \$\begingroup\$ This appears to give the same probability for A and B. Imagine it for an alphabet of only 3 letters ABC: p(C) = 1/2, p(B) = 1/4, p(A) = 1 - 1/2 - 1/4 = 1/4. \$\endgroup\$ – trichoplax Aug 13 '16 at 11:43
  • \$\begingroup\$ Unless A is also chosen with a probability of 1/2 when it is reached, with the possibility of wrapping back round to Z? If it always terminates when it reaches A then p(A) = p(B) = 1/2^(25). \$\endgroup\$ – trichoplax Aug 13 '16 at 11:48
1
\$\begingroup\$

Julia, 33 bytes

f()=['A':'Z'][ceil(26*√rand())]

Applies the square root to RNG.

Graph showing distribution: enter image description here Generated with:

using Gadfly
plot(x=ceil(26*√rand(10000)),Geom.histogram)
\$\endgroup\$
1
\$\begingroup\$

ForceLang, 64 bytes

Noncompeting, uses language features (the string.char method) that postdate the question.

set s math.sqrt 676.mult random.rand()
io.write string.char 65+s
\$\endgroup\$
  • \$\begingroup\$ Override header: Valid but noncompeting (ignore this - it's just to let the leaderboard snippet know) \$\endgroup\$ – trichoplax Aug 13 '16 at 0:16
1
\$\begingroup\$

Actually, 7 bytes

ú#;╚♀MJ

Try it online!

This answer uses the same approach as Dennis's Jelly answer.

Explanation:

ú#;╚♀MJ
ú#       lowercase English alphabet, as a list (this is to dodge a bug with the shuffle command)
  ;╚     duplicate, shuffle
    ♀M   pairwise maximum
      J  random element
\$\endgroup\$
1
\$\begingroup\$

perl, 21 bytes

say+(A..Z)[rand$$%27]

Needs -M5.010 or -E to run :

perl -E 'say+(A..Z)[rand$$%27]'


How it works : (A..Z) is an array. $$ is process ID, which introduces some randomness. $$%27 is a number between 0 and 26 (inclusive, and roughly evenly spread). Calling rand on that produces a number between 0 and 26 (inclusive at 0, exclusive at 26), but biased towards smaller numbers. We then use that number for an array lookup, which we print.

Opportunities for improvement :

$$ is random, but not cleanly random. We know that the chance of getting particular numbers differs, but not by much, and not in ways that are easy to predict cleanly. Therefore, the possibility of getting different each letter is probably not identical, but it's very close. If (and only if) we accept that probability{ $$%26 == x } is different for all x, then we could replace "rand$$%27" with "$$%26".

Alternately, we could replace "rand$$%27" with (for example) "26*sin$$", and save one character. This gives a much less regular spread than simply "$$%26":

 perl -E 'for(1..32768){say+(A..Z)[26*sin$_]}' | sort | uniq -c
    832 A
   3269 B
   1648 C
   1360 D
   1183 E
   1133 F
   1043 G
   1071 H
    959 I
    978 J
    985 K
    939 L
    926 M
    950 N
    924 O
    939 P
    985 Q
    977 R
    959 S
   1072 T
   1043 U
   1135 V
   1182 W
   1360 X
   1647 Y
   3269 Z

But if you check, you'll see that it's not perfect. There are three pairs of numbers that have the same probability as each other: K&Q, I&S, L&P.

(If you use actual PIDs, then depending on what processes are actually running, and depending on the highest allowable pid on your machine, you might get more clashes, or less.)

"15*sin$$" works better, only 2 clashes - but that is still 2 clashes. (Any number between 13 and 26 will work, in the sense of producing all 26 letters, because sin(x) spans -1 to +1 and perl treats negative number array look-ups as starting from the array end).

There might be some way of getting all 26 numbers, with different probabilities, using less (or no more) than the 8 characters that "rand$$%27" needs. But if so, it isn't coming to me.

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

Clojure, 62 bytes

(loop[i 65](if(or(> i 89)(neg?(rand)))(char i)(recur(inc i))))

This is somewhat stupid shortcut. The probability of exact zero double - the only (rand) to not break on condition (pos? x) - is around 1/(2^62) [source].

So chance for letter char(65+N) is around (1/(2^62))^N, slightly higher for Z (because it is last).

With (rand-int 2) - 6 more bytes - it becomes testable.

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

Perl 6, 30 bytes

{('a'..'z'Zxx 1..*).flat.pick}

Explanation:

# bare block lambda
{
  (

    'a' .. 'z' # the alphabet
    Z[xx]      # zipped 「Z」 using the list repeat operator 「xx」
    1 .. *     # with 1 to 26

    # ((a)(b b)(c c c)(d d d d)(e e e e e)...

  )
  .flat # flatten it from a list of lists into a single list
  .pick # pick an element from the list
}
\$\endgroup\$
1
\$\begingroup\$

Pip, 8 bytes

(zr*r*h)

z is a built-in for the lowercase alphabet, h for 100. r is a special variable: each time it is evaluated, it generates a random number with uniform distribution, 0 <= r < 1. Thus, r*r*h squares the uniform distribution and scales it a hundredfold (saving a character over using 26). The result is used to index into z, auto-truncated and with index wrapping.

Try it here (frequency chart of 100,000 runs, takes about 30 seconds).

\$\endgroup\$
  • 1
    \$\begingroup\$ It wasn't immediately obvious to me that folding the unevenly distributed range of 100 values into a range of 26 values would maintain the property of having all probabilities distinct (especially as the frequency chart has some letters appearing similar numbers of times). I checked and can confirm that the probabilities (assuming a perfect uniform distribution as a starting point) are all distinct: \$\endgroup\$ – trichoplax Aug 17 '16 at 14:04
  • \$\begingroup\$ 0.122257379845413, 0.063400059354823, 0.0534956778948075, 0.0482511384937917, 0.04481748680079, 0.0423168133306285, 0.0403735674712969, 0.0387960044737913, 0.0374744304593056, 0.0363408541731965, 0.0353505332076944, 0.0344726212707875, 0.0336850437718526, 0.0329715122041705, 0.0323196958133572, 0.0317200547970168, 0.0311650693170861, 0.0306487146656851, 0.0301660946708953, 0.0297131797962578, 0.0292866162765532, 0.028883584540095, 0.023514130401554, 0.0231759623356073, 0.0228547631421794, 0.0225490114913639 \$\endgroup\$ – trichoplax Aug 17 '16 at 14:04
1
\$\begingroup\$

Lua, 100 99 bytes

s="A"math.randomseed(os.time())for i=66,90 do
s=.6<math.random()and string.char(i)or s end
print(s)

Not really competitive, but still an interesting algorithm I think.

\$\endgroup\$
  • \$\begingroup\$ Due to there being a probability of 0.5 each time through the loop, it looks like the end of the loop leaves the same probability for Z as for Y (since there can be no halving of the probability when only Z is left). You could get around this by using a figure other than 0.5, which would make the solution valid without adding any bytes. \$\endgroup\$ – trichoplax Aug 17 '16 at 21:57
  • 1
    \$\begingroup\$ @trichoplax I see what you mean. Changed it, thanks! \$\endgroup\$ – Trebuchette Aug 18 '16 at 23:09
1
\$\begingroup\$

IA-32 machine code, 12 bytes

Hexdump:

0F C7 F0 F6 E4 88 E0 D4 1A 04 61 C3   

Source code:

rdrand eax
mul ah
mov al, ah
aam 26
add al, 'a'
ret

Or, as a C expression (assuming r is a uniformly generated random number of type uint32_t):

(r & 0xff) * ((r >> 8) & 0xff) / 0x100 % 26 + 'a'

Running this expression on values in the range 0...65535 gives the probabilities of the letters:

z 1968
y 2012
x 2068
w 2076
v 2134
u 2140
t 2194
s 2209
r 2233
q 2292
p 2335
o 2358
n 2410
m 2437
l 2475
k 2545
j 2571
i 2654
h 2696
g 2738
f 2818
e 2922
d 2955
c 3098
b 3241
a 3957
\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, 14 bytes

See this meta post for information about the code page.

(?351)⊃⎕A/⍨⍳26

Gives instant results. Works by selecting an evenly distributed random character among "ABBCCCDDDD..."

(?351) RandInt(1,351)
picks among
⎕A "ABC...Z"
/⍨ element-by-element replicated
⍳26 {1, 2, 3, ..., 26} times

TryAPL online!

\$\endgroup\$
  • \$\begingroup\$ @MartinEnder. Ouch, that may affect a few submissions. \$\endgroup\$ – Adám May 14 '17 at 19:51
  • \$\begingroup\$ Well I think some of the leaderboard implementations are a bit smarter than the one I used here. \$\endgroup\$ – Martin Ender May 14 '17 at 19:53
1
\$\begingroup\$

MATL, 12 bytes

1Y2toY"tnYr)

Try it online!

Explanation

1Y2          % Pushes the alphabet A...Z
   to        % Make a numeric copy [65 ... 90]
     Y"      % Run length decoding: 65 A's, 66 B's, etc. Let's call this [S]
        nYr  % Random number between 1 and length of [S], let's call this [i]
       t   ) % Index into a copy of [S] at the [i]'th position. Implicit display.

The selection from [S] is according to a uniform distribution, but since each letter in the alphabet occurs a distinct number of times in [S], the resulting distribution is also distinct for each letter in the alphabet.

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

Python 3, 58 bytes

from random import*;print(chr(randint(65,randint(65,91))))

it is like that perl answer.

\$\endgroup\$
  • \$\begingroup\$ 57 bytes: r=__import__('random').randint;print(chr(r(65,r(65,91)))) \$\endgroup\$ – FlipTack Dec 22 '16 at 19:28
  • \$\begingroup\$ post yourself? @Flp.Tkc \$\endgroup\$ – Destructible Lemon Dec 22 '16 at 22:15
  • \$\begingroup\$ I just golfed the import statement, you did the main algorithm, so post it yourself :) \$\endgroup\$ – FlipTack Dec 22 '16 at 22:52
0
\$\begingroup\$

k4, 15 bytes

*1?a|-26?a:.Q.A

A port of @Dennis's Jelly answer.

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

JavaScript ES6, 42 Bytes, modified from user81655's solution:

_=>(0|10+26*Math.random()**2).toString(36)
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 26 22 bytes

I think it's unfair that most of these are biased towards earlier letters.

Here's one that prints Z the most:

?CHR$(90-RND(RND(27)))
\$\endgroup\$
0
\$\begingroup\$

Javascript, 49 bytes

f=()=>String.fromCharCode(65+Math.random()**2*26)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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