# Choose The Powerball Numbers!

## C#, 153 bytes 140 bytes

Thanks to "McKay":

string.Join(" ",Enumerable.Range(1,69).OrderBy(e=>Guid.NewGuid()).Take(5).Concat(Enumerable.Range(1,26).OrderBy(e=>Guid.NewGuid()).Take(1)))

153 bytes solution:

string.Join(" ",Enumerable.Range(1,69).OrderBy(e=>Guid.NewGuid()).Take(5))+" "+string.Join(" ",Enumerable.Range(1,26).OrderBy(e=>Guid.NewGuid()).Take(1))

Simple solution using Linq and shuffling using GUID.

• Don't need the string join in the powerball number portion and you'd save bytes either caching the order by delegate or a lot more by using random for the powerball number – pinkfloydx33 Jan 12 '16 at 17:50
• And if you concat the sequences instead of string concatenation, you don't need to specify the space3 times, it would be more efficient as well. e.g. string.Join(" ", ....take(5).Concat(....Take(1))) – McKay Jan 12 '16 at 18:49

# Pyth - 1314 13 bytes

Major golfing possible, this was just a FGITW.

jb>5.SS69hO26
• Could you explain the code so it can be checked that it's really uniform? – Masclins Jan 12 '16 at 8:07
• You can change the <... 5 to >5.... Final code jb>5.SS69hO26 – Blue Jan 13 '16 at 19:54

# Brachylog, 40 bytes

1:5e,68{Irandom(I)+1=ZZ=w,"
"w}\;25:1&

### Explanation

Brachylog doesn't have a built-in for random numbers (yet...) so we have to use an SWI-Prolog predicate for that: random/1. We can input SWI-Prolog code in Brachylog using backquotes.

1:5e,                             \  § Enumerate from 1 to 5 using \ (backtrack), which
§ evaluates to false and thus constructs a loop

68{                         }   § Declare sub-predicate 1 and call it with 68 as input

Irandom(I)+1=Z             § Z is the arithmetic expression 'random(I) + 1' where
§ I is the input of the sub-predicate

Z=w,         § Evaluate Z and write it
"\n"w    § Write a new line

;                                    § Else (we will reach this after the 5th iteration of
§ the enumeration, since \ is always false)

25:1&                                § Call the sub-predicate 1 with 25 as input

# JavaScript (ES6), 10686 84 bytes

F=(s=new Set,r=Math.random)=>s.size<5?F(s.add(r()*69+1|0)):[...s,r()*26|0+1].join

Since we can't uniquely sample randoms in JavaScript, this works by creating a Set (which only holds unique values), recursively adding randoms (1-69) until there are 5 unique ones, appending a random number (1-26), then joining and returning it all out.

# Elixir, 83 Bytes

Enum.reduce Enum.take_random(1..69,5)++[Enum.random(1..26)],fn(x,a)->"#{a} #{x}"end

When just IO.putsing an array of integers, Elixir will interpret the integers as characters and therefore output some string instead of the desired powerball numbers. So, we have to reduce the integer array down to a string.

### Ruby, 4743 39 bytes

puts *(1..69).to_a.sample(5),rand(26)+1

I think it can be golfed more, but I'll work on that once I'm done admiring how pretty this code looks, considering.

It works pretty much the same way as everything else: Take an array of the numbers 1 to 69, shuffle them, get the first five, output those, then output a random number between 1 and 26.

I went through a few iterations before posting this:

puts (1..69).to_a.shuffle.first(5).join(' ')+" #{rand(26)+1}"  #61
puts (1..69).to_a.shuffle[0..5].join(' ')+" #{rand(26)+1}"     #58
puts (1..69).to_a.shuffle[0..5].join('<newline>'),rand(26)+1   #52
puts *('1'..'69').to_a.shuffle[0..5],rand(26)+1                #47
puts *('1'..'69').to_a.sample(5),rand(26)+1                    #43

(where <newline> is replaced with an actual newline)

EDIT: Whoops, didn't see the preexisting Ruby answer. I stumbled on sample and was scrolling down to edit my answer, but then I saw it... Oh well. My final score is 43 bytes, but I'll keep golfing a little to see how well I can do.

# Mathematica, 64 bytes

StringRiffle@Append[Range@69~RandomSample~5,RandomInteger@25+1]&

Quite simple.

• The output has braces and commas. – Charles Jan 12 '16 at 16:35
• -1 invalid for above reason – CalculatorFeline Apr 16 '16 at 20:01
• StringJoin=""<>##& – CalculatorFeline Apr 18 '16 at 2:44
• @CatsAreFluffy Actually fixed this time. Just mixed up my functions... – LegionMammal978 Apr 18 '16 at 10:09
• I think you could shorten it if you only use Range/RandomSample and transposed it over two lists instead of using RandomInteger. Something like RandomSample[Range[#1],#2]&@@@{{69,5},{26,1}}] – Xanderhall Apr 19 '16 at 11:29

# Perl 5, 59 bytes

{say for(sort{-1+2*int rand 2}1..69)[0..5];say$==1+rand 25} It's a subroutine; use it as: perl -M5.010 -e'sub f{...}f' • You can use -E instead of -M5.010 -e – andlrc Jan 13 '16 at 1:05 • Cant you can replace -1+2*int rand 2 with rand>.5?1:-1? – andlrc Jan 13 '16 at 1:07 • And shouldn't you be able to save another few by replacing ;say$== with ,\$== – andlrc Jan 13 '16 at 1:12
• @dev-null Thanks! The -M5.010 doesn't count anyway so I didn't bother abbreviating it. I think I tried a comma instead of another say and it didn't work. But the new sort rule is a good idea, thanks. I'll test it when I have a chance and edit it in. – msh210 Jan 13 '16 at 3:31

## PHP, 65 bytes

<?=join(' ',array_rand(array_flip(range(1,69)),5))." ".rand()%26;

Thanks to the other PHP answer on this page. I wrote up a program on my own, and it turned out to be the exact same answer as the one written by Samsquanch, which drove me to take it a step further to save a few bytes.

If anyone can figure out a way to append one array to another in here that's less than the 5 bytes it takes me to join the powerball number on after, I would greatly appreciate it, cause it's driving me nuts! The best I could come up with would be after array_rand and before join, having a statement something like +[5=>rand()%25], but that's an extra byte over just concatenating it on after.

<?=                                                              // This represents an inline 'echo' statement
range(1,69)                    // Get an array of all numbers from 1 to 69 inclusive
array_flip(           )                   // Swap the keys and values.
array_rand(                       ,5)                // Get a random subset of five keys.
join(' ',                                     ).rand()%26     // Concatenate the array with spaces, along with the powerball number

Run it through the command line. Sample:

C:\(filepath)>php powerball.php

Output:

12 24 33 67 69 4

## PARI/GP, 71 70 bytes

apply(n->print(n),numtoperm(69,random(69!))[1..5]);print(random(26)+1)

It generates a random permutation of [1..69], then takes the first 5.

Unfortunately this is an inefficient user of randomness, consuming an average of 87 bytes of entropy compared to the information-theoretic ideal of 3.5. This is mainly because the entire permutation is generated instead of just the first 5 members, and also because the perms are ordered (losing lg 5! =~ 7 bits). Further, random uses a rejection strategy rather than using arithmetic coding. (This is because PARI uses Brent's xorgen, which is fast enough that the overhead from more complicated strategies is rarely worthwhile.)

There are three 'obvious' changes which do not work under the current (2.8.0) version of gp. random and print could be stored in variables, and print could be called directly rather than via the anonymous -> function:

r=random;apply(p=print,numtoperm(69,r(69!))[1..5]);p(r(26)+1)

Together these would save 9 bytes. Unfortunately both functions are valid without arguments, and hence are evaluated immediately rather than stored, so these do not compute the desired output.

## Intel x86 Machine code, 85 bytes

¿I ±‰ø1Ò»E ÷óB‰Ðè+ ‰þÑç÷ƒÇþÉ„Éuâ‰ø1Ò» ÷óB‰Ðè
0äÍ¸ Í±ëÆÔ
00†Äˆã´ÍˆØÍ° ÍÃ

Well it does sometimes print the same numbers if so, just try again by pressing a key.

Compile with:

nasm file.asm -o file.bin

Make sure to align it to a floppy size (add zeros at the end) in order to mount it to a vm (it does not need any operating system).

Disassembly:

BITS 16
ORG 0x7c00

mov di,73 ;starting seed
mov cl,5 ;we need five numbers

loop:

mov ax,di

xor dx,dx
mov bx,69
div bx
inc dx
mov ax,dx

call print_rnd_number

mov si,di
shl di,1

dec cl

test cl,cl
jne loop

mov ax,di

xor dx,dx
mov bx,26
div bx
inc dx
mov ax,dx

call print_rnd_number

xor ah,ah
int 0x16
mov ax,0x0002
int 0x10
mov cl,5
jmp loop

print_rnd_number:
aam
xchg al,ah
mov bl,ah
mov ah,0x0E
int 0x10
mov al,bl
int 0x10
mov al,' '
int 0x10
ret
• Welcome to Programming Puzzles & Code Golf! Seeing machine code is always impressive, but if it prints repeated numbers on occasions, I'm afraid your submission is invalid. – Dennis Jan 12 '16 at 19:21
• Yes it is but I just wanted to share this :) – ByteBit Jan 12 '16 at 20:00
• I understand, but we recently discussed this, and the community's consensus was that invalid answers should either be fixed or removed. – Dennis Jan 12 '16 at 20:24
• I'll undownvote if you fix; please ping me. – lirtosiast Jan 13 '16 at 23:33

# C, 142 bytes

i,x[6],n;main(j){for(srand(time(0));i<6;n-i?0:printf("%d ",x[i]),i++)for(x[n=j=i]=(69-i/5*43)*(rand()/2147483647.)+1;i<6&&j--;)i-=x[i]==x[j];}

Not terribly happy with this solution as it feels like there should be more golfing opportunity. I'll look at it again tomorrow with fresh eyes. Try it here.

# Swift, 165 bytes

import UIKit;var a=[Int]();for i in 0...5{func z()->Int{return Int(arc4random_uniform(i<5 ?68:25)+1)};var n=z();while i<5&&a.contains(n){n=z()};a.append(n);print(n)}

Can quickly be run in an Xcode Playground.

EDIT: Current problem here is that it's theoretically possible for this to run forever in the while loop if arc4random_uniform somehow keeps pulling the same number. The odds of that happening, for any significant length of time, are probably better than the odds of winning the Powerball.

• How does it guarantee that there won't be any duplicated values? – supercat Jan 13 '16 at 21:29
• @supercat, it didn't before, but now it does. – timgcarlson Jan 13 '16 at 23:17

# Perl 6,  32  31 bytes

# space is necessary so that the parens denote a list
# rather than being part of the call to put
put (pick(5,1..69),(1..26).pick) # 32 bytes
# 25 35 57 67 62 24␤

Turning it into a function that returns a string, I can remove 4 bytes (put␠) while only adding 3 ({~ })

{~(pick(5,1..69),(1..26).pick)} # 31 bytes

Usage:

say {...}().perl; # use .perl to prove it returns a string
# "25 35 57 67 62 24"

### If a function were allowed to return a list of the values, the following would also work.

( Otherwise it would be the same as above but within { } )

• function that returns a single flat list

{|pick(5,1..69),(1..26).pick} # 29 bytes
# (25,35,57,67,62,24)

• function that returns a list with the first 5 numbers in a sub list

{pick(5,1..69),(1..26).pick} # 28 bytes
# ((25,35,57,67,62),24)

• function that returns a list with the first 5 in a sub list, and the Powerball in another sub list

{pick(5,1..69),pick 1,1..26} # 28 bytes
# ((25,35,57,67,62),(24,))

# Seriously, 35 bytes

My first attempt at an answer in a golfing language.

Feels longer than it should have to be.
The repetition could likely be removed with W, but it seems to be broken in the online interpreter and I don't want to post untested code.

Too bad { doesn't work on lists.

Code:

:70:1xi J{. J{. J{. J{. J{.:26:Ju.

Hex dump:

3a37303a317869204a7b2e204a7b2e204a7b2e204a7b2e204a7b2e3a32363a4a752e7f

Explanation:

:70:1x                       # Push a list of the numbers 1-69
i                            # Explode list
J{.                         # rotate stack random(len(stack)) times and pop/print
J{. J{. J{. J{.             # same thing 4 more times
:26:Ju.                      # print random number in 1-26
# (7F) unprintable, terminate without explicit printing

Online interpreter

# Lua, 96 Bytes

A simple solution, using a table as a set by putting the value inside it as table[value]=truthy/falsyto be able to check if they are inside it or not.

I lose 5 bytes because I have to set the first value of my table, else I won't go inside the while(o[n])loop and will simply output n before using the random function. As Lua uses 1-based tables, I also have to force it to put its first value to the cell [0], otherwise I couldn't output a 1.

m,n,o=math.random,0,{[0]=0}for i=1,5 do while(o[n])do n=m(69)end o[n]=0 print(n)end print(m(26))

### Ungolfed:

m,n,o=math.random,0,{[0]=0}
for i=1,5
do
while(o[n])
do
n=m(69)
end
o[n]=0
print(n)
end
print(m(26))

## C++, 252 bytes

### Golfed:

#include<iostream>
#include <random>

int main(){std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> dis(1,69);for(int c=0;c<=5;c++){std::cout<<dis(gen)<<" ";}int x=dis(gen);while(x>26||x<1){x=dis(gen);}std::cout<<x;return 0;}

### Ungolfed:

#include<iostream>
#include <random>

int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 69);
for(int c = 0; c <= 5; c++){
std::cout<<dis(gen)<<" ";
}
int x = dis(gen);
while (x > 26 || x < 1){
x = dis(gen);
}
std::cout<<x;
return 0;
}