New answers tagged

0

Japt, 14 13 bytes Takes input as a single integer array. Weighted very heavily towards the earlier pairs in the sequence. iT Èej}a@m+Xö Try it


2

Husk, 15 bytes ḟΛṗmoSe+⁰L↓x1İπ Try it online! Note: requires input of a random 'seed' to generate different output on different runs Husk is completely deterministic, and has no built-in random number generator. However, some googling indicates that the digits of Pi have been shown to be 'random' (in a certain statistical sense), if a set of constraints ...


0

Japt, 25 bytes Warning: This will hang your browser if the random value is unfavorable. It does however not have an infinite loop, just a very, very long runtime in bad cases. This relies on the given assumption that a satisfactory pair is guaranteed to exist. Starting from a random whole number ensures that any pair can be returned, after that we just move -...


0

Jelly,  16  15 bytes 2‘XḂ$¿,³Ä¹ßẒẠ$? A full program that prints a random, valid pair - if given enough time and recursion stack/memory (recursion depth is set to \$2^{30}\$ in TIOs interpreter/Dennis' GitHub; but we seg-fault before then generally). Try it online! Note that there is a seg-fault for high n (or always for those invalid n which are odd while n+...


1

VBScript, 147 bytes Sub m(s) randomize:i=0 if s="*" then msgbox "Hello World" if s=" * " then msgbox int(2147483647*Rnd) if s="*+*" then do loop while 1 end if end sub


1

APL (Dyalog Unicode), 40 bytes {⍵{⍵[⊃(?∘⍴⌷⊢)⍸⍺=-∘.-⍨⍵]}(⊢~∘.×⍨)1↓⍳3300} Not entirely certain that I understood the problem statement. I run out of memory with primes larger than around 3300. Not great, but it's my first stab at it. ⍵{...} ⍝ passing user input to nested function as left argument ⍵[...] ⍝ retrieving primes for given index values ⊃(?∘⍴⌷⊢...


6

Haskell, 132 130 bytes f$[2..]>>= \x->[2..x] f(x:y)n=do b<-randomIO;last$f y n:[pure(x,x+n)|b,p x,p$x+n] p n=all((>0).mod n)[2..n-1] import System.Random Try it online! The relevant function is f$[2..]>>= \x->[2..x], which takes n as input and returns a random pair of primes (x,x+n). Definitely not the shortest possible answer, but ...


3

MATL, 16 15 bytes `,1r/kYq]-G-}2M Outputs the two primes in decreasing order. Try it online! Explanation Let \$q(k)\$ denote the \$k\$-th prime: \$q(1)=2\$, \$q(2)=3\$, \$q(3)=5\$, \$\ldots\$ The code generates two random primes \$r_1, r_2\$ independently with distribution \$\quad \displaystyle \Pr[r_i = q(k)] = \frac 1 {k\cdot(k+1)}; \quad i = 1,2; \quad k ...


7

R, 111 105 103 73 70 69 bytes Edit: -2 bytes thanks to Robin Ryder (as well as -5 more bytes for the now-discarded previous version) function(s){while(sum(!(z=T+s*0:1)%%rep(2:T,e=2))>1)T=1+rpois(1,9) z} Try it online! Picks random numbers n until (n,n+s) are both prime.


0

JavaScript (Node.js), 68 bytes f=n=>(g=d=>k%--d?g(d):d>1)(k=2/Math.random()|0)|g(k+=n)?f(n):[k-n,k] Try it online! Still shorter than Arnauld's wrong answer


6

05AB1E, 13 bytes Generates a random integer \$k\$ until both \$k\$ and \$k+n\$ are prime. [΂[>₄Ω#}DpP# Try it online! Runs the program 1000 times and shows the unique results. Commented: [ # loop: ΂ # push [0, input] [ } # loop: > # increment both integers ₄Ω # draw a random digit of ...


1

Husk, 35 20 bytes :z!İπ*⁰²¨(ηøṘΔż₀Tüp) Try it online! Husk is completely deterministic, and has no random number generator. So, on the face of it, "anything involving randomness ... can't be answered in Husk without considerable extra effort or rule-bending"(1). However, the constraint that 'lines should be chosen at random' seems (to me) to be ...


1

VBScript, 122 bytes Randomize:n=n-1 For i=0 to n For Each x in Array(a,b,c,d) WSH.Echo x(Int(4*Rnd)) Next Next WSH.Echo "(Now read bottom up)" Header a = Array("I am a terrible person","Everything I do is useless","I don't deserve to be loved","I will never succeed at anything") b = Array("It's foolish ...


2

R, 63 bytes function(n,l)c(l[sample(4,4*n,T)+0:3*4],"(Now read bottom up)") Try it online!


1

GolfScript, 14/15 bytes {100rand.}do]` Try it online! Uses rand built-in to generate random numbers from 0-99, inside a do loop to terminate once a 0 is generated. The output of the loop is collected into an array and formatted for output. If the brackets and quotation marks invalidate the output, here is another version that sacrifices 1 byte for cleaner ...


2

Zsh, 8 bytes, \$ \approx \frac{1}{10}\$ chance of success >$$ : *4 Try it online! Determines whether the process ID ends in 4.


1

Ruby, 10 bytes 1.div rand Can't do 1/rand, since 1/0.0 in Ruby is Infinity, not an error. Try it online!


0

CSASM v2.2.1.2, 39 bytes func main: extern Random.Next print ret end CSASM has a built-in System.Random object that gets initialized to new System.Random() when CSASM.Core.Sandbox.Main(System.Reflection.MethodInfo, int, string[]) is called. The default System.Random constructor initializes the seed to Environment.TickCount, which is different every time the ...


1

CSASM v2.2.1, 74 bytes func main: .lbl a push 100 extern Random.Next(i32) dup print.n brtrue a ret end Commented: func main: .lbl loop ; Get a random number in [0, 99] push 100 extern Random.Next(i32) ; Duplicate it so it can be printed and then checked dup print.n ; Keep looping as long as the i32 ...


1

VBScript, 51 bytes Randomize Do r=Int(100*Rnd) msgbox r Loop Until r=0


1

><>, 50 bytes 701>x1>$:2*}*+ }1-:?!v}30.> ^\0/.;?=0oan:%*aar< Try it online! After many iterations, I got down to this version. Pretty happy with it, although I will admit I'm not convinced it's optimal. Explanation The ><> language doesn't have a natural builtin for generating a random number in a range. The only source of ...


1

Japt, 11 bytes Lö OpU ?ß:P Try it online! Outputs delimited by newlines, with a trailing newline after the 0 Lö # Get a random number from [0...99] and store it as U OpU # Print U and a newline ? # If U is non-zero: ß # Run the program again :P # Otherwise end with no further output


2

Pxem, Filename: 23 17 bytes + Content: 0 bytes = 23 17 bytes, depends on ASCII-compatible encoding. Filename: d.!.t.w.m.r.c.n,.o.a Content: empty Commented d.t # heap = 100 .w # loop .m.r # push(random(min=0,max=heap-1)) .c # dup() .n # printf("%d",pop()) ,.o # putchar(',') .a # break if pop()==0


1

Vyxal, 5 bytes {k/℅₴ No trying it online It was a smart choice adding all of the two byte constants of 05ab1e and Jelly.


2

BRASCA, 13 12 bytes 1[`/\`1?#$o] Try it online! Explanation 1[ ] - While 1: `/\` - Push \ and / 1? - Push rand(0,1) #$ - If truthy, swap the top two items, else skip. o - Print it!


3

Jelly, 5 bytes Ø^XȮß Try it online! XØ^ß is a byte shorter, but prints a 0 before the slashes. X Choose a random element from Ø^ "/\", Ȯ print it, ß then call this link again.


1

AppleSoft BASIC, 33 bytes 33 bytes, verified here 1?MID$("\/",RND(1)+1.5,1);:GOTO1 35 bytes, using CHR$ conversion similar to C64. 1?chr$(47+45*int(2*rnd(1)));:goto1


0

Zsh, 42 bytes for ((;;))printf ${(#)$((RANDOM%2?47:92))} try it online


Top 50 recent answers are included