New answers tagged

1

><>, 53 bytes "+ ␞␞"r4[r}:}>1-$:\ <~x<1 }-1}o:}+^?:$o/oa;!?:{ ^ \- (Replace ␞ with the corresponding byte 0x1E; it is replaced with a printable unicode character here for clarity.) Try it online! The first + always starts at position 30. Explanation Initialization and Iteration Cleanup "+ ␞␞"r4[r}:} Let's examine ...


1

Commodore BASIC (Tested on a Commodore 64, but recommended 128 80 columns mode) 72 tokenised BASIC bytes 0X=29:FORI=.TOX:PRINTSPC(X)"O":X%=3*RND(.):ONX%GOSUB1,2:NEXT:END 1X=X-1:RETURN 2X=X+1:RETURN How it works X is declared as the spacing and is reused in the FOR/NEXT loop - the . is a slightly faster way of using 0 in Commodore BASIC, so the ...


1

Python 3.8, 64 bytes from random import* for x in[0]*30:print(' '*randint(29,31)+'+') Try it online!


5

Vyxal, 12 bytes 30:(kɽ℅+:×꘍, Try it Online! 30: # Push 30 twice ( # Iterate over one copy (30 times) + # Increment the number on the stack (initially 30) by... ℅ # A random choice from kɽ # [-1, 0, 1] : # Duplicate ×꘍ # Prepend that many spaces to an asterisk , # And output, ...


3

Vyxal ROM, 7 bytes {&›¥℅|, Try it Online! Explanation: { | # While... &› # ...Increment the register (initially 0)... ¥ # ...Retrieve value n from the register... ℅ # ...Random value from range[0..n]... | # ...Returns true, do: , # Print the input string


0

Excel, 62 bytes =MID(CONCAT((RANDARRAY(A1*128)>0.5)*1),SEQUENCE(A1,,,128),128) Link to Spreadsheet 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 ...


1

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.


1

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


2

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


2

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


2

Scala, 49 bytes def f(n:Int)=Seq.fill(n)(BigInt(128,util.Random)) Try it online!


1

R, 136 bytes a=scan(,"");cat(intToUtf8(sample(rep(c(10,utf8ToInt(a)),2),136,T))) a=scan(,"");cat(intToUtf8(sample(rep(c(10,utf8ToInt(a)),2),136,T))) Try it online! The "read-a-second-copy-and-sample-from-it" approach is shamefully copied from Robin Ryder's answer: please upvote that one. Here, however, rather than counting the instances of each ...


2

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


1

Julia 1.0, 50 bytes (a=:(print.(rand("(a=:($(a)))|>eval", 50))))|>eval Try it online! based on this quine: (a=:(print("(a=:($(a)))|>eval")))|>eval Try it online!


3

Julia 1.0, 18 bytes !n=rand(UInt128,n) Try it online! sorry, boring useful built-in


2

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.


1

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.


1

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.


2

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


3

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.


2

Ruby, 29 Bytes ->n{n.times.map{rand 2**128}} Try It Online


3

Befunge-98 (PyFunge), 57 37 bytes &'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, ...


12

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


5

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!


1

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


2

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


5

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


2

R, 174 bytes cat(sample(el(dimnames(a<-table(c("\n",el(strsplit(scan(,""),"")))))),174,T,a),sep="") cat(sample(el(dimnames(a<-table(c("\n",el(strsplit(scan(,""),"")))))),174,T,a),sep="") Try it online! The function sample performs discrete random sampling. Itcan take as 4th argument a vector prob of probabilities. The vector prob does not need to be ...


1

Vyxal r, 14 bytes 3»∧l%≥D⋎v»τiB℅ Try it Online! Takes input as a number.


0

Japt -P, 11 bytes Gö6 msG i'# Test it


3

Vyxal Ṫ, 8 7 bytes \#k66ƈṅ Try it Online! \# # Push a # 6ƈ # Choose six... k6 # Hex digits ṅ # Appended to the #


2

HTML+JS, 186 <pre style="letter-spacing:-3.5px;line-height:9px;;white-space: pre-wrap;line-break: anywhere;" onclick="setInterval(_=>this.innerHTML+='/\\'[2*Math.random()|0])">C l i c k m e !</pre>


1

Ly, 104 bytes n`n,n,>1[p6[>&p<6[>16?<,]p>arppp&+s>l<p<,]p>>&s<&l<<&s>>>>&p&l<a0Isp>rlGfp<s>flLfp<&+sp>rlLfp**sp<<<l!]> Try it online! The specific steps in this code are pretty straightforward, the only trick is keeping the data organized on ...


0

Vyxal, 29 bytes ÷ʀ℅$19r℅":∑⁰$-:£4ḭw3ẋf:∑¥$-Wf Try it Online!


4

R 4.1, 112 bytes Using the new shorthand notation of defining functions (\(x) expr): r=\(l,h,t){x=l;while(sum(x)<t|min(x)>l|max(x)<h){x=matrix(sample(6,24,r=T),nr=6);x=rowSums(x)-apply(x,1,min)};x} Explanation Generate 6x4 matrix (nrow=6) with random number 1-6: x=matrix(sample(6,24,r=T),nr=6) Calculate the sum over the rows and subtract the row-...


1

Java (JDK), 175 bytes (l,h,t)->{int r[]=new int[6],i,j,m,d,s,x=0,T=0;for(;x<3|T<t;)for(x=T=i=0;i<6;T+=r[i++]=s-=m,x|=s<h?s>l?0:1:2)for(m=7,s=j=0;j++<4;s+=d+=Math.random()*6,m=d<m?d:m)d=1;return r;} Try it online!


2

Ruby, 91 bytes ->l,h,t{1while(w=(k=*1..6).map{(k*4).sample(4).sort[1,3].sum};w.min>l||w.max<h||w.sum<t);w} Try it online!


1

Python 3, 138 bytes from random import* def f(l,h,t): while l<min(s:=[3+sum(d:=choices(range(6),k=4))-min(d)for _ in[0]*6])or h>max(s)or t>sum(s):0 return s


3

Python 3, 139 137 bytes Recursive function which returns a tuple of integers. from random import* f=lambda l,h,t,*k:k*(t<=sum(k)>max(k)>=h>=l>=min(k))or f(l,h,t,*eval('sum(sorted(map(randint,[1]*4,[6]*4))[1:]),'*6)) Try it online!


5

Troll, 135 bytes Right tool for rolling dice, but not necessarily for golfing. function s(x)=w:=x pick1;x:=x--w;'w<>x&call s(x) function f(l,h,t)=call s(repeat x:=6#sum largest3 4d6until h<=max x&l>=min x&t<=sum x) Try it online! (Click on Make random rolls. and the result will appear a bit further down.) The first function ...


4

JavaScript (ES6), 138 bytes (l,h,t)=>eval(F="while(F||S-3<t)[F=3,S=3,...A=[3,3,3,3]].map(n=>(A.map(m=_=>n+=m<(v=Math.random()*6|0)?v:m=v),S+=n-=m,F&=n>l|2*(n<h),n))") Try it online! As pointed out by @Shaggy, a recursive version is shorter (134 bytes). But it will fail when the odds are low ... whereas the above version ...


5

APL (Dyalog Unicode), 40 bytes Full program. Prompts for total, then high, then low. {(+/-⌊/)?6 4⍴6}⍣((∨/⎕≥⊣)∧(∨/⎕≤⊣)∧⎕≤1⊥⊣)⍬ Try it online! {…}…⍬ apply the following lambda to the empty list dummy argument:  6 4⍴6 cyclically reshape 6 into a 6-row, 4-column matrix  ? roll those dice  (…) apply the following tacit function to that:   +/-⌊/ the sum minus ...


1

Python 3.8 (pre-release), 198 bytes from random import* def g(l,h,t,r=range): while sum(k:=list(sum(sorted(randint(1,6)for _ in r(4))[1:])for _ in r(6)))<t or(n:=list(map(sum,zip(*((i<=l,i>=h)for i in k)))))[0]*n[1]!=1:pass return k Not fast but gets the job done. It is quite possible that the last test won't finish as it needs perfect rolls. On ...


3

Charcoal, 36 bytes W∨›Iζ↨υ¹∨›Iη⌈υ‹Iθ⌊υ≔EE⁶E⁴⊕‽⁶⁻Σκ⌊κυIυ Try it online! Link is to verbose version of code. Explanation: W∨›Iζ↨υ¹∨›Iη⌈υ‹Iθ⌊υ Repeat until the initially empty predefined list fails to satisfy any of the conditions that its total is less than the third input, its highest member is less than the second input or its lowest member is greater than ...


3

Factor + dice math.unicode combinators.short-circuit.smart, 153 bytes [ [ 6 [ 4 [ ROLL: 1d6 ] replicate natural-sort rest Σ ] replicate 3dup { [ Σ <= nip ] [ nip minmax '[ _ _ between? ] ∀ ] } && ] [ drop ] until 2nip ] Try it online! It's a quotation (anonymous function) that takes a low-high pair (like { 10 15 }) and a sum target from the data ...


9

Japt, 28 bytes Takes input as t, l & h. @§Xx*Xd§V*Xd¨W}a@6Æ4ÆÒ6öÃÍÅx Try it @§Xx*Xd§V*Xd¨W}a@6Æ4ÆÒ6öÃÍÅx :Implicit input of integers U=t, V=l & W=h @ :Left function, taking an array X as input § : Is U less than or equal to ... Xx : X reduced by ...


Top 50 recent answers are included