# Make an array of 128 bit integers

Given an input value $$\n\$$, construct an array of $$\n\$$ random 128 bit (unsigned) integers. The integers should be uniformly random.

Your code can use any in built random number generation function it has or you can implement your own. Clearly this challenge is harder (and more fun) in lower level languages.

• If we decide to implement our own RNG, how good does it have to be? (returning the same number repeatedly is technically uniform, if the input seed is) Jul 25 at 13:40
• @thedefault better than that :) any rng you find on the internet will be fine Jul 25 at 13:56
• Well, @thedefault.'s rng can be found on the internet... Jul 25 at 15:41
• Also, what can we do if language lacks 128-bit integers? Is returning array of 64-bit integers that's twice as long as requested and uniformly random allowed? Jul 25 at 15:44
• You can still print out 128 bit integers in decimal as some of the solutions are doing. It's not ideal but better than nothing. Jul 25 at 15:49

# x86-64 machine code, 10 bytes

Fortunately, there is a built-in RNG. Requires the RDRAND instruction.

00000000: c1e1 020f c7f0 abe2 fac3                 ..........

Disassembled:

shl ecx, 2
label:
rdrand eax
stosd
loop label
ret

Outputs to a pre-allocated buffer in rdi. n is passed in ecx (arrays with 2^30 elements and more are not supported).

Try it online! (a helper function is needed because of the unusual calling convention)

• It's worth noting explicitly that it outputs to a pre-allocated buffer at ES:RDI! Common modern operating systems use a flat segment model, but x86-64 doesn't require/guarantee that. For one byte extra, you can optimize the performance of the code by generating and storing 64-bit values (RDRAND rax; STOSQ). This also allows switching from SHL ecx, 2 (which is 3 bytes) to SHL ecx, 1 (which is 2 bytes), or even ADD ecx, ecx (still 2 bytes). Unfortunately, the 64-bit version of RDRAND is 1 byte larger, as is the 64-bit version of STOS, so it's a net change of +1 byte. Jul 26 at 22:08
• Also, forgot to mention in my previous comment that the RDRAND instruction is an optional part of the x86-64 ISA. It is supported by Intel Ivy Bridge and later. This is not an issue for Code Golf, of course, but whenever I've used it, I've preferred to call out that requirement in the title, since that's a stipulation of what environment I'm targeting with my submission. Jul 27 at 3:26
• @CodyGray I don't know very much about segments, but Google results say that the cs/ds/ss/es segments always start at zero and end at 2^64 in long mode (and these results don't seem OS-specific). I tried using 64-bit rdrand/stosq, but I also saw that it was 1 byte longer. Jul 27 at 4:38

# Vyxal, 7 bytes

ƛ₇ƛ₀℅;B

Try it Online!

ƛ       # Map...
₇ƛ  ;  # Map 128 to...
₀℅   # Random choice from digits of 10
B # Convert to base 10

Or if it's allowed not to complete within the lifetime of the universe, 5 bytes:

ƛ₇Eʁ℅

Try it Online!

ƛ     # Map...
₇E   # 2 ** 128
ʁ  # Range (0...x)
℅ # Choose a random item

It's trying to generate a 2128-item array, do you think it's gonna finish any time soon?

• So ₇E℅ gives you a random digit from the decimal value of 2¹²⁸?
– Neil
Jul 31 at 12:25
• @Neil Yeah, although I'm trying to change that - see the second-to-last item of this tasklist Jul 31 at 20:06

# JavaScript (Node.js),  72  70 bytes

Saved 2 bytes thanks to @RedwolfPrograms

The helper function g builds a 128-bit integer, one bit at a time. This seems a bit long...

n=>Array(n).fill(128n).map(g=k=>k--&&BigInt(Math.random()<.5)|2n*g(k))

Try it online!

• I think this saves 2 bytes, by using Array and fill: tio.run/… Jul 25 at 17:22

&'wa+0> #;\1-:9j;+1?;#*2\_$.1-:!#@_1j Try it online! Note that the input has to have a trailing space because of a bug in the interpreter. The part generating a random 128-bit number bit by bit is explained below: 'wa+0> #;\1-:9j;+1?;#*2\_\.@ Try it online! 'wa+ push loop counter, initially 129 0 push initial number 0 > if the IP is coming from the right, turn it around #; # skips the next instruction \ swap to the loop counter 1-: decrement it and push an additional copy 9j skip over the next 9 instructions _ if the loop counter is 0, go right: \. swap to the number and print it @ terminate the execution _ else, go right: *2\ swap to the number and multiply by 2 ? go into a random direction, if up or down is selected, ? gets executed again ; ; if right is selected, jump to the next semicolon and continue with the next iteration > #; ;+1 if left, increment the number and go to the next iteration Animation with 9 instead of 128 bits: # Python 2 54 53 bytes: Try it online! lambda n:map(randrange,[4**64]*n) from random import* 69 bytes: Try it online! lambda n:[int(os.urandom(i).encode('hex'),i)for i in[16]*n] import os # Python 3 54 bytes: Try it online! lambda n:[*map(randbits,[128]*n)] from secrets import* 57 56 bytes: Try it online! lambda n:[*map(randrange,[4**64]*n)] from random import* 70 bytes: Try it online! lambda n:[int.from_bytes(os.urandom(i),'big')for i in[16]*n] import os • 1<<128 can also be 4**64, which saves a byte. Jul 25 at 8:09 • You might also want to add eval('uuid.uuid4().int,'*n); it works in both versions of Python as far as I can tell. Jul 25 at 8:25 • Is uuid random? Isn't it the same every time? Jul 25 at 8:51 • @Anush uuid4 is a function to generate a random uuid (docs) – ovs Jul 25 at 8:52 • Type 4 UUIDs aren't uniformly random; some bits are fixed. Jul 25 at 15:00 # Julia 1.0, 18 bytes !n=rand(UInt128,n) Try it online! sorry, boring useful built-in • A great advert for julia. But why such an ancient version? Jul 25 at 19:34 • that's the latest version available on TIO, this will of course work for any 1.x version Jul 25 at 19:35 # PowerShell, 57 bytes 41 bytes @(1..$n|%{(Random 1.0)*("2*"*128+1|iex)})

Explanation: 1..$n enumerates integers from 1 to$n, % is an alias to ForEach-Object, Random decimal between 0 and 1.0, multiply by 2^128 through this clever obfuscation. @() encapsulates the result in an array, save 3 bytes if a actual array isn't needed.

Try it online!

Original (57 bytes):

@(1..$n|%{(Get-Random -Mi 0.0 -Ma 1)*[Math]::Pow(2,128)}) • Welcome to Code Golf, and nice first answer! Be sure to check out our Tips for golfing in Powershell page for ways you can golf your program Jul 25 at 22:06 • Hi @ssh2ksh! Nice first answer! Make sure you get the$n parameter as an argument. For example: Try it online!. I also changed the power calculation, and removed the @() that is not needed in this context Jul 25 at 23:44
• Rearranged to have less tb digits and less stuff after Random Try it online! Jul 26 at 2:52

# Factor, 33 31 bytes

[ [ 128 2^ random ] replicate ]

Try it online!

• replicate Create a sequence given a number of elements and a generating quotation.
• 128 2^ 2128
• random Return a uniformly random number (via Factor's default Mersenne Twister PRNG) from 0 to whatever number is on top of the data stack minus one.

# Ruby, 29 Bytes

->n{n.times.map{rand 2**128}}

# Try It Online

• 27 bytes Jul 25 at 23:43

# Raku, 17 bytes

{roll ^4**64: $_} Try it online! ^4**64 is the range of numbers [0, 2¹²⁸). The roll method returns randomly selected numbers from that range, in the quantity of$_, the input argument.

# C (gcc), 68 42 bytes

f(n,a)char*a;{for(n*=16;n--;)a[n]=rand();}

Try it online!

• takes an 0 array and the number requested as input

• the array I taken as *char to be manipulated byte by byte so that we set every 8bit to rand() which is RAND_MAX e.g. At least 32767 and thus guaranteed to affect every bit

• numbers printed as decimal thanks to this good answer on the topic I copy-pasted-adapted

• The upper halves of these random numbers are always very small (and not printed in the TIO link). I think you can replace 64 with 128 and remove the %4. Jul 25 at 16:28
• It still doesn't work (and the upper halves still aren't printed in the TIO link) Jul 25 at 17:37
• Seems to segfault on TIO Jul 25 at 20:22
• @Anush yes I operated on 32 chars exceeding a little bit the array lol Jul 25 at 20:32
• try this f(n,a)int*a;{for(n*=4;n--;)a[n]=rand();} Aug 3 at 15:31

# Scala, 49 bytes

def f(n:Int)=Seq.fill(n)(BigInt(128,util.Random))

Try it online!

# Wolfram Language (Mathematica), 25 24 bytes

–1 byte thanks to Bubbler!

RandomInteger[4^64-1,#]&

Try it online!

I wrote this and even I can't find anything interesting to say about it

• Change 2^128 to 4^64 to save a byte. Jul 26 at 3:43
• The shortest answer containing the word random? Everything is interesting in at least one way :) Jul 26 at 5:40
• @Anush I applaud your glass-half-full attitude :) Jul 26 at 7:03

# Excel, 62 bytes

=MID(CONCAT((RANDARRAY(A1*128)>0.5)*1),SEQUENCE(A1,,,128),128)

Excel doesn't handle 128 bit integers. What this returns is technically n strings of 128 random 1s and 0s, which could be interpreted as n 128 bit binary integers. If Excel did handle 128 bit integers, then answer would be =RANDARRAY(A1,,,2^128-1,1) which is 26 bytes.

# R, 24 22 bytes

Edit: -1 byte thanks to pajonk

runif(scan())*4^64%/%1

Try it online!

Output is an integer uniformly selected from the range 0...2^128-1.
R does not natively support 128-bit inteters, so the data type of the output is a floating-point representation (click here for a TIO link that dispays all the digits, instead of displaying in scientific notation). Note though that although 128-bit numbers can be represented using floating-point variables, rounding inaccuracies can occur when more than 53 bits are required.

If floating point output (with its associated rounding inaccuracies) isn't Ok, we can output the exact value in the form of a vector of binary digits for 35 bytes (or flattened into a string of bits for 41 bytes).

If neither floating point nor binary output are Ok, then base-R will struggle a bit, but we can still manage by constructing string of random 0-9 digits (so including leading zeros), and checking that its lexicographically smaller than "340282366920938463463374607431768211456", for a whopping 117 113 bytes:
for(i in 1:scan())show({while((a=Reduce(paste0,sample(0:9,39,T)))>"340282366920938463463374607431768211456")0;a})

• How do the floating point numbers have enough precision for 128 bits? Jul 26 at 5:42
• @Anush - They don't. It wasn't clear to me from the challenge whether this was a requirement (if it is it should probably be specified explicitly, see here). However, I do realise that this was an obvious limitation of the approach: hence the two other versions. Jul 26 at 5:56
• Thanks. It's a very nice set of answers all in. My thoughts were that the uniform requirement would already imply full precision. Is that wrong? Jul 26 at 6:01
• -1 byte Jul 26 at 7:32
• @pajonk - Thank you! Jul 26 at 8:06

# JavaScript (Browser), 99 bytes

n=>crypto.getRandomValues(a=new BigUint64Array(n*2)).reduce((r,v,i)=>i&1?[v<<64n|a[i^1],...r]:r,[])

This feature is introduced eailer this month by this post. And it currently requires a recent version of Firefox / Chromium Nightly build to run.

It is a sad story that words crypto, getRandomValues, BigUint64Array are too long for golfing.

const f=

n=>crypto.getRandomValues(a=new BigUint64Array(n*2)).reduce((r,v,i)=>i&1?[v<<64n|a[i^1],...r]:r,[])

{
let len = 5;
let arr = f(len);
for (let i = 0; i < arr.length; i++) {
console.log(arr[i] + '')
}
}

• No TIO possible? Jul 26 at 6:55
• @Anush No, this feature is added this month while TIO is outdated yearly.
– tsh
Jul 26 at 7:14
• Which feature exactly was added? Jul 26 at 7:29
• crypto.getRandomValues only support (U)Int(8|16|32)Array but not Big(U)Int64Array before. Try to passing BigUint64Array to crypto.getRandomValues will cause an exception before.
– tsh
Jul 26 at 7:32
• @Anush Also, window.crypto is only available in browsers. If you are using Node.js 15+, you may need to write require('crypto').webcrypto instead of crypto.
– tsh
Jul 26 at 7:37

# MathGolf, 5 bytes

É♣ów]

Try it online.

Explanation:

É      # Loop the (implicit) input amount of times,
# using the following three characters as inner code-block:
♣     #  Push builtin 128
ó    #  Pop and push 2**128
w   #  Pop and push a random integer in the range [0,2**128)
]      # After the loop, wrap the entire stack into an array
# (after which the entire stack is output implicitly as result)

# Jelly, 7 bytes

Ø⁷2*X’)

Try it online!

A monadic link taking a single argument and returning a list of random 128-bit unsigned integers of that length.

## Explanation

) | For each value from 1..n
Ø⁷      | 128
2*    | 2 ** that
X   | Random Int from 1 to that
’  | Subtract 1

If the output can be from 1 to $$\2^{128}\$$ rather than 0 to $$\2^{128} - 1\$$, which for practical purposes is almost the same given how unlikely it is to generate the extremes, I can shave off a byte.

# Perl 5, 51 bytes

sub{join('',map chr rand 256,1..16*pop)=~/.{16}/sg}

Try it online!

Creates n random 128 bit integers. Or n strings of 16 random 8 bit chars. How to print the array elements as decimal numbers can be seen by clicking the "Try it online" link.

# Japt, 7 bytes

ÇMq2pIÑ
Ç       // Create the range [0..U) where U is the input, then map each value to
Mq     // a random non-negative integer smaller than
2    // 2
p   // to the power of
IÑ // 64 * 2.

Try it here.

• I don't think the output format has enough precision to represent all integers between 0 and 2^128-1. Jul 26 at 1:26

# CJam, 20 19 bytes

ri{L{2mr+}128*2bN}*

Try it online!

# CJam, 27 24 bytes (Old method)

ri{8,{G*YG#(mr\m<}%:+N}*

Try it online!

Edit: had a better idea using the "repeat N times" operator. Saved 3 characters.

Edit 2: tried a completely new algorithm. Saved 4 characters.

Edit 3: went for the string manipulation side of things. Saved 1 character.

# Charcoal, 10 bytes

ＩＥＮ‽Ｘ²¦¹²⁸

Try it online! Link is to verbose version of code. Explanation:

Ｎ         Input integer
Ｅ          Map over implicit range
Ｘ²¦¹²⁸  2¹²⁸
‽        Random integer (0-indexed)
Ｉ           Cast to string
Implicitly print

# Racketsrfi/27, 45 bytes

(for/list([i n])(*(random-real)(expt 2 128)))

Try it online!

depends on srfi/27 package

• Welcome to code golf, and nice answer! Just so you know, when a submission uses a certain package, it's common practice to put those next to the name of the language, e.g. "Racket srfi/27, 45 bytes" Aug 2 at 12:14
• @AaronMiller I don't think that's common practice. I'd probably assume the string next to the name is a command line argument. And a note in the post is sufficient. Aug 3 at 4:06

# C (gcc), 59 bytes

f(n){int*a=malloc(n*16),i=0;for(;i<n*4;)a[i++]=rand();n=a;}

Try it online!

# 05AB1E, 7 bytes

LεžyoÝΩ

No TIO, since it won't finish anyway.

Explanation:

L        # Push a list in the range [1, (implicit) input-integer]
ε       # Map each to:
žy     #  Push builtin 128
o    #  Pop and push 2 to the power this 128
Ý   #  Pop and push a list in the range [0, 2^128]
Ω  #  Pop and push a random item from this list
# (after which the result is output implicitly)