25
\$\begingroup\$

The task is simple. Write an interpreter for the language *.

Here's a bigger link to the wiki.

There are only three valid * programs:

  • * Prints "Hello World"
  •  *  Prints a random number between 0 and 2,147,483,647
  • *+* Runs forever.

The third case must be an infinite loop according to the specifications in this question

Input:

  • The input can be taken via any acceptable input method by our standard I/O rules
  • It will always be one of the above programs

Output:

  • The first case should print exactly Hello World, with or without a trailing line break.
  • For the second case, if your language's integer maximum is smaller than 2,147,483,647, use your language's integer maximum
  • The first and second cases can print to any acceptable output by our standard I/O rules.
  • The third case should not give any output.

Scoring:

As this is , the shortest answer, in bytes, wins.

\$\endgroup\$
8
  • 7
    \$\begingroup\$ When you say 'between 0 and 2,147,483,647', is that inclusive or exclusive? (E.g., is 0 a valid output?) \$\endgroup\$
    – Chas Brown
    Aug 9, 2019 at 21:46
  • 7
    \$\begingroup\$ Changing the spec after posting a challenge and invalidating existing solutions is an automatic -1 from me. \$\endgroup\$
    – Shaggy
    Aug 10, 2019 at 7:00
  • 2
    \$\begingroup\$ For languages that don't have a built-in way of generating a random number, is there an acceptable way to satisfy the "print a random number" requirement? \$\endgroup\$ Aug 10, 2019 at 22:44
  • 2
    \$\begingroup\$ If our language's integers have no, or a higher, maximum may we use a higher upper bound? \$\endgroup\$ Aug 10, 2019 at 23:43
  • 8
    \$\begingroup\$ @Shaggy I'm not seeing any rule changes in the wiki for the question, only a space to a non-breaking space (check the markdown tab), because the SE markdown renderer wasn't rendering it, but looking at the original revisision, it's clear it should be normal spaces, and the "hack' is only done for SE markdown renderer issues \$\endgroup\$
    – Ferrybig
    Aug 11, 2019 at 20:49

35 Answers 35

25
\$\begingroup\$

*, 0 bytes


Since * has no way of reading input, the default rules allow specifying that the input must be given by concatenating it onto the program.

(... I think. There's an "at least twice as many upvotes as downvotes" condition that I don't have the rep to verify).

\$\endgroup\$
6
  • 5
    \$\begingroup\$ Your linked meta is indeed a currently accepted site standard (+31 -7). \$\endgroup\$ Aug 10, 2019 at 19:32
  • 3
    \$\begingroup\$ @A__: It looks to me like it must have been designed specifically to satisfy someone's proposed definition of 'programming language' ("You can write hello world!" "You can write an infinite loop!" "You can write a program that doesn't always do the same thing!"). \$\endgroup\$ Aug 11, 2019 at 3:44
  • \$\begingroup\$ I believe that technically Malbolge is not a programming language either then. \$\endgroup\$
    – Bob Jansen
    Aug 11, 2019 at 10:16
  • 1
    \$\begingroup\$ Malbolge is programming language for finite automata, the same as *, and, for example, Befunge-93. Therefore Malbolge is formal programming language, the same as *, technically the same as recursively enumerable languages when it comes to programming language definition (albeit formal languages are less powerful). \$\endgroup\$ Aug 11, 2019 at 17:05
  • 2
    \$\begingroup\$ Downvote from me, because honestly, this answer is so boring it's actually already a Standard Loophole, even if we ignore the question whether * is a programming language \$\endgroup\$ Oct 14, 2019 at 11:56
9
\$\begingroup\$

R, 69 bytes

switch(scan(,""),"*"="Hello, World!"," * "=sample(2^31,1)-1,repeat{})

Try it online!

switch tries to match the named arguments and if there's no match, selects the first unnamed one after the first, which in this case is the infinite loop repeat{}.

\$\endgroup\$
8
\$\begingroup\$

Jelly,  21  20 bytes

ḊOSØ%HX’¤“½,⁾ẇṭ»¹Ḃ¿?

A monadic Link accepting a list of characters.

Try it online!

vL’... also works (see below).

How?

ḊOSØ%HX’¤“½,⁾ẇṭ»¹Ḃ¿? - Link: list of characters   e.g.: "*"        or  " * "    or  "*+*"
Ḋ                    - dequeue                          ""             "* "         "+*"
 O                   - to ordinals                      []             [42,32]      [43,42]
  S                  - sum                              0              74           85
                   ? - if...
                  ¿  - ...if-condition: while...
                 Ḃ   -    ...while-condition: modulo 2  0              0            1
                ¹    -    ...while-true-do: identity                                85
                     -                                  0              74           (looping)
        ¤            - ...then: nilad followed by link(s) as a nilad:
   Ø%                -    literal 2^32                                 2^32
     H               -    half                                         2^31
      X              -    random integer in [1,n]                      RND[1,2^31]
       ’             -    decrement                                    RND[0,2^31)
         “½,⁾ẇṭ»     - ...else: dictionary words        "Hello World"

Alternative

vL’... - Link: list of characters                 e.g.: "*"        or  " * "    or  "*+*"
 L     - length                                         1              3            3
v      - evaluate (left) as Jelly code with input (right)
       -                                                1^1            3^3          (3^3+3)^3
       -                                                1              27           27000
  ’    - decrement                                      0              26           26999
   ... - continue as above                              "Hello World"  RND[0,2^31)  (looping)
\$\endgroup\$
6
\$\begingroup\$

C (gcc), 66 63 bytes

Thanks to attinat for the -3 bytes.

I only have to check the second character: if the LSB is set, it's a + (thus the program is "*+*") and the program loops. After that, if it's a NUL, the program was "*" and we display Hello World; otherwise, it displays a random value (" * ", the only other option left.)

f(char*s){for(s++;*s&1;);printf(*s?"%d":"Hello World",rand());}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ 63 bytes \$\endgroup\$
    – att
    Aug 9, 2019 at 22:21
  • \$\begingroup\$ Shave a byte: f(char*s){*++s&1?f(s-1):printf(*s?"%d":"Hello World",rand());} \$\endgroup\$ Aug 10, 2019 at 22:24
  • \$\begingroup\$ Pedantic note: rand is not guaranteed to return a sufficiently large value; RAND_MAX and INT_MAX are not guaranteed to be the same (and are not on real world compilers, e.g. Visual Studio's RAND_MAX is 32767, while INT_MAX is [on modern x86 derived systems] the 2147483647 value specified in the OP's question). \$\endgroup\$ Aug 12, 2019 at 14:45
  • \$\begingroup\$ @ShadowRanger that is completely true, but considering that >90% of all C-based CGCC entries rely on undefined and unspecified behaviour I'm not worried about that! I also didn't feel like implementing a code-golfed LCG today. :-) \$\endgroup\$
    – ErikF
    Aug 12, 2019 at 16:53
6
\$\begingroup\$

Keg, -lp, -ir 30 26 25 24 20 19 bytes

!1=[_“H%c¡“| =[~.|{

-1 byte using flags

Try it online!

Answer History

?!1=[_“H%c¡“| =[~.|{

Try it online!

Shortened Hello World to dictionary string

!1=[_Hello World| =[~.|{

Try it online!

I never ceased to be amazed at the power of Keg. Credits to user EdgyNerd for another byte saved.

Prior Versions

_!0=[Hello World|\*=[~.|{

Try it online!

Credit to user A__ for the extra byte saved.

Old version

?!1=[_Hello World| =[__~|{

Essentially, takes the input program and:

  • Checks to see if the input length is 1, printing "Hello World" if true
  • Checks to see if the last character is a space, and prints a random number
  • Otherwise runs an infinite loop

Then implicitly print the stack.

?                               #Get input from the user
 !1=                            #Compare the stack's length to 1
    [_Hello World           #Push "Hello, World!" to the stack
                     | =        #See if top item is a space
                        [__~|{  #If so, generate a random number, otherwise, infinite loop.

4 bytes saved due to the fact that hello world doesnt need punctuation.

Try it online! Old version

Try it online! New version

\$\endgroup\$
11
  • \$\begingroup\$ You can cut off 4 bytes, you don't need the comma or exclamation mark in "Hello World". \$\endgroup\$ Aug 10, 2019 at 2:14
  • 1
    \$\begingroup\$ Now I have to learn another unpopular language in order to answer challenges here normally. \$\endgroup\$
    – user85052
    Aug 10, 2019 at 6:26
  • 1
    \$\begingroup\$ -1 bytes: TIO. I am glad that I did not lose my ability to golf in Keg. \$\endgroup\$
    – user85052
    Aug 10, 2019 at 6:36
  • \$\begingroup\$ @A__ Enjoying Keg being on TIO? \$\endgroup\$
    – lyxal
    Aug 10, 2019 at 6:48
  • 2
    \$\begingroup\$ 24 bytes :) \$\endgroup\$
    – EdgyNerd
    Aug 10, 2019 at 8:49
5
\$\begingroup\$

Japt, 22/25 bytes

The first solution is for the original spec which had *<space> as the second programme and the other is for the updated spec which arbitrarily changed it to <space>*</space>, with thanks to EoI for the suggested "fix".

Both throw an overflow error upon entering the infinite loop of the third programme but, theoretically, with enough memory (which we may assume for the purposes of ), would run forever.

Å?¢?ß:2pHÉ ö:`HÁM Wld

Try programme 1
Try programme 2
Try programme 3

Å?UÎ>S?ß:2pHÉ ö:`HÁM Wld

Try programme 1
Try programme 2
Try programme 3

\$\endgroup\$
4
  • \$\begingroup\$ I think the second program is "[SPACE]*[SPACE]", not "[SPACE]*", so your program doesn't work \$\endgroup\$
    – Gymhgy
    Aug 10, 2019 at 6:51
  • \$\begingroup\$ @EmbodimentofIgnorance, at the time I posted, the second programme in the spec was *<space>. Don't have time to update now. \$\endgroup\$
    – Shaggy
    Aug 10, 2019 at 6:58
  • \$\begingroup\$ You can fix it in three bytes with UÌ>S instead of ¢ on the second ternary \$\endgroup\$
    – Gymhgy
    Aug 10, 2019 at 7:08
  • \$\begingroup\$ @Downvoter, please have the courtesy to leave a comment. \$\endgroup\$
    – Shaggy
    Aug 12, 2019 at 9:17
5
\$\begingroup\$

Python 2, 103 93 89 87 bytes

I combined my earlier answer with Chas Browns's answer and got something a few bytes shorter.

The random number will be between 0 and 2**31-1 inclusive.

from random import*
i=input()
while'*'<i:1
print["Hello World",randrange(2**31)][i<'!']

Try it online!

Previous versions:

103 bytes

from random import*
exec['print"Hello World"','while 1:1','print randint(0,2**31-1)'][cmp(input(),'*')]

93 bytes

from random import*
i=cmp(input(),'*')
while i>0:1
print["Hello World",randint(0,2**31-1)][i]
\$\endgroup\$
9
  • \$\begingroup\$ Save 2 bytes by replacing randint(0,2**31-1) with randrange(2**31). \$\endgroup\$
    – Chas Brown
    Aug 9, 2019 at 21:53
  • \$\begingroup\$ while'*'<i saves 2 \$\endgroup\$ Aug 10, 2019 at 0:12
  • \$\begingroup\$ Save another byte by changing randrange(2**31) to getrandbits(31) (the latter returns long, not int, but print will print the str form, not the repr form, so the trailing L won't be there). \$\endgroup\$ Aug 12, 2019 at 14:18
  • \$\begingroup\$ Relatively inexperienced with site, so quick clarification: Are you allowed to require your input to be quoted? i=input() only works if the inputs are quoted, if you just input plain */ * /*+*, it would die with a SyntaxError (because input includes an implicit eval); you'd need to input '*'/' * '/'*+*' (or equivalent with double-quotes instead). I didn't see anything obvious on the standard I/O rules that would allow this, which might mean you'd need to use raw_input(), costing four bytes. \$\endgroup\$ Aug 12, 2019 at 14:27
  • \$\begingroup\$ @ShadowRanger input() takes a string as input and evaluates it. I'm not really adding to the input, I'm merely taking a string as input, and strings have quotes. This is pretty standard, in the same way that I can take an array like [1,2,3] instead of as a delimited string that I then have to split and parse. The goal of the site is not to make input strict, it's to make I/O easy so we can focus code on the challenge at hand. \$\endgroup\$
    – mbomb007
    Aug 12, 2019 at 15:49
3
\$\begingroup\$

JavaScript (ES7), 66 bytes

s=>s[1]?s<'!'?Math.random()*2**31|0:eval(`for(;;);`):'Hello World'

Try it online! (Hello World)

Try it online! (random number)

Try it online! (infinite loop)

\$\endgroup\$
4
  • \$\begingroup\$ Would x=(z=>x())&&x() not work for -1byte from the infinite loop code, assuming a browser with no max call stack size? \$\endgroup\$ Aug 10, 2019 at 15:52
  • \$\begingroup\$ @GezaKerecsenyi We could just call ourself (like this) but I'm not sure that would be acceptable. \$\endgroup\$
    – Arnauld
    Aug 10, 2019 at 15:55
  • \$\begingroup\$ that's fair. I wonder if there is some obscure browser out there that just keeps going (at least, until RAM runs out) \$\endgroup\$ Aug 10, 2019 at 15:57
  • 1
    \$\begingroup\$ @Arnauld, theoretically, that would run forever given infinite memory, which we may assume for code golf. \$\endgroup\$
    – Shaggy
    Aug 10, 2019 at 20:47
3
\$\begingroup\$

Jelly, 23 21 bytes

OS¹Ḃ¿ịØ%HX’;““½,⁾ẇṭ»¤

Try it online!

A monadic link taking a single argument and returning Hello World, a random 31 bit integer or looping infinitely as per the spec.

All options: * * *+*

Explanation

O                     | Convert to codepoints
 S                    | Sum
  ¹Ḃ¿                 | Loop the identity function while odd 
     ị              ¤ | Index into the following as a nilad:
      Ø%              | - 2 ** 32
        H             | - Halved
         X            | - Random integer in the range 1..2**31
          ’           | - Decrease by 1 
           ;          | - Concatenated to:
            ““½,⁾ẇṭ»  |   - "", "Hello World"
\$\endgroup\$
3
\$\begingroup\$

Befunge-93, 54 bytes

~"*"-_~1+#^_"dlroW olleH">:#,_@.%*2**:*::*88:*`0:?1#+<

Try it online!

Annotated:

~"*"-                      _                ~1+                   #^_        "dlroW olleH">:#,_    @      .%*2**:*::*88:   *`0:             ?1#+<
Compare first      If equal, go right       Compare second       If equal,        Output          Exit    Modulo by 2^31   If less than      Add 1
character to       Otherwise, go left       character to       loop forever   "Hello World"                 and output     0, multiply     a random amount
'*'                and wrap around          -1 (EOF)                                                                         by 0            of times

The randomness is not uniform. At each increment there is a 50% chance to stop incrementing.

\$\endgroup\$
3
\$\begingroup\$

Python 2, 91 89 88 bytes

from random import*
def f(p):
 while'*'<p:p
 print['Hello World',getrandbits(31)][p<'!']

Try it online!

2 bytes thanks to Jonathan Allan; 1 byte thx to ShadowRanger.

\$\endgroup\$
3
  • \$\begingroup\$ while'*'<p saves 2 \$\endgroup\$ Aug 9, 2019 at 22:25
  • \$\begingroup\$ getrandbits(31) saves a byte over randrange(2**31). \$\endgroup\$ Aug 12, 2019 at 14:21
  • \$\begingroup\$ Nice! Didn't know about getrandbits... \$\endgroup\$
    – Chas Brown
    Aug 12, 2019 at 18:48
3
\$\begingroup\$

PowerShell, 60, 56 bytes

switch($args){*{'Hello World'}' * '{random}*+*{for(){}}}

Pretty dumb version, the only golfing technique here is omitting Get- in Get-Random.

UPD. Stripped down to 56 bytes by removing quotes, thanks to veskah!

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

///, 72 bytes

/+*/+*// */a random number between 0 and 0//*/"Hello World"/

Try it online!

Here I go again, adding another cheating entry. Was I really going write my own pseudo-random generator just for this though... and in ///?!

Instead, I interpreted the Print instruction very literally, as a good minion should. Everything after Print is outputted:


* Prints "Hello World"

*"Hello World", yes, even with the quotation marks. Otherwise, I'd admit to being aware of the quotation-marks-denote-literal-string convention, which wasn't explicitly stated in the question, and doesn't exist in the language with which I'm submitting. And then I wouldn't be able to cheat with the "Print a random number" part. So, I'm feigning ignorance... Shhh, don't tell!

​ * ​ Prints a random number between 0 and 2,147,483,647

*a random number between 0 and 0. Lucky for me, although they didn't add the whole non-deterministic, different-number-every-time shtick in the rules, they did say "if our language's integer maximum is smaller than 2,147,483,647" we can use our language's "integer maximum" instead, so I did... There are no integer types in ///, only strings. So the integer maximum is 0, sort of...
Well, it shaves off a few bytes!

*+* Runs Forever

*+*. There's no Print instruction in this one so I can interpret it like a normal human. Replacing a pattern with itself (or another string containing itself), is the standard way to loop forever, and it's as succinct as it gets!


Note

To golf it a bit, /// looks for +* & ​ *​ rather than the whole thing. This works just as well. For the infinite loop, nothing ever prints because the replacing never stops, so you'll never find out that it's prepending an infinitely long list of * to the beginning every time it replaces. As for printing 'a random number,' we just don't replace one of the spaces, leaving a trailing space at the end of the output. Luckily, this hurts no one and is not noticeable. Plus, how's an ignoramus like me to know how many of the trailing spaces at the end of the Print instruction, until the margin, were on purpose :p

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Sorry, this question seems to have provoked a mischievous mood from me today! \$\endgroup\$
    – AviFS
    May 18, 2020 at 19:41
2
\$\begingroup\$

Perl 5 -p, 43 39 bytes

$_=/ /?0|rand~0:/\+/?redo:"Hello World"

Try it online!

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

C# (Visual C# Interactive Compiler), 71 bytes

s=>{for(;s=="*+*";);return"*"==s?"Hello World":new Random().Next()+"";}

Try it online!

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

Ruby -n, 47 bytes

puts~/ /?rand(1<<31):~/\+/?loop{}:"Hello World"

Try it online!

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

Wolfram Language (Mathematica), 65 bytes

#/.{"*"->"Hello World"," * "->RandomInteger[2^31-1],_:>0~Do~∞}&

Try it online!

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

Java (JDK), 83 bytes

s->{for(s=s.intern();s=="*+*";);return"*"==s?"Hello World":(-1>>>1)*Math.random();}

Try it online!

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

Brachylog, 26 23 bytes

l₃∈&hṢ∧2^₃₁-₁ṙw∨Ḥ⊇ᶠ³⁶tw

Try it online!

Takes the program as a string through the input variable, and ignores the output variable. Heavily exploits the guarantee that the input is only ever one of the three valid programs: any length-three input will behave like either " * " or "*+*" depending on whether or not the first character is a space, and any other input will behave like "*".

l₃                         The input has length 3
  ∈                        and is an element of something,
   &h                      and the input's first element
     Ṣ                     is a space
  ∈                        (if not, try some other thing it's an element of),
      ∧2^₃₁-₁              so take 2,147,483,647 and
             ṙw            print a random number between 0 and it inclusive.
               ∨           If the input's length isn't 3,
                Ḥ⊇ᶠ³⁶tw    print the 36th subsequence of "Hello, World!".
\$\endgroup\$
1
  • \$\begingroup\$ Oops, wrong "Hello World"--fixing now \$\endgroup\$ Aug 9, 2019 at 21:41
2
\$\begingroup\$

PHP, 51 bytes

for(;'*'<$l=$argn[1];);echo$l?rand():'Hello World';

Try it online! (Hello World)

Try it online! (Random Number)

Try it online! (Infinite Loop)

Takes second character of input which can be '', '*' or '+'. In case of '+' the '*'<'+' will be true and the loop will be infinite, else, after the loop, "Hello World" or a random number is shown. The rand() automatically outputs a number between 0 and getrandmax() which uses defined RAND_MAX in standard C library and by default is 2147483647 on most platforms/environments, including TIO.

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

APL (Dyalog Unicode), 39 bytesSBCS

Anonymous prefix lambda.

{'+'∊⍵:∇⍵⋄' '∊⍵:⌊2E31×?0⋄'Hello World'}

Try it online!

{ "dfn"; is the argument:

'+'∊⍵: if plus is a member of the argument:

  ∇⍵ tail recurse on argument

' '∊⍵ if space is a member of the argument:

  ?0 random float (0–1)

  2E31× scale to (0–2³¹)

   floor

'Hello World' else return the string

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

05AB1E, 21 bytes

'*KgDi[ë<ižIL<Ω딟™‚ï

Try it online. (NOTE: The random buildin is pretty slow with big lists, so it might take awhile before the result is given.)

Explanation:

'*K           '# Remove all "*" from the (implicit) input
   g           # Get the length of what's remain (either 0, 1, or 2)
    D          # Duplicate this length
     i         # If the length is exactly 1:
      [        #  Start an infinite loop
     ë<i       # Else-if the length is 2:
        žI     #  Push builtin 2147483648
          L    #  Create a list in the range [1,2147483648]
           <   #  Decrease each by 1 to make the range [0,2147483647]
            Ω  #  Pop and push a random value from the list
               #  (after which the top of the stack is output implicitly as result)
     ë         # Else:
      ”Ÿ™‚ï    #  Push dictionary string "Hello World"
               #  (after which the top of the stack is output implicitly as result)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ”Ÿ™‚ï is "Hello World".

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

Pyth, 32 bytes

It/Jw\*#;?tlJOhC*4\ÿ"Hello World

Try it online!

Explanation (Python-ish)

I                                   # if 
  /Jw\*                             #    (J:=input()).count("*"))
 t                                  #                             - 1:
       #;                           #     try: while True: pass;except: break;
         ?                          # if (ternary)
           lJ                       #    len(J):
             O                      #     randInt(0,                    )
               C                    #                int(     , 256)
                *4\ÿ                #                    4*"ÿ"
              h                     #                                + 1
                    "Hello World    # else: (implicitly) print "Hello World"
\$\endgroup\$
2
  • \$\begingroup\$ This prints a number between 0 and 2^32, not 0 and 2^31. A shorter way to write hC*4\ÿ is ^2 32, but in order for the solution to be correct, you should use ^2 31 instead. Also, use z instead of Jw, saves 1 more byte. And your explanation skips the line with t right before lJ. \$\endgroup\$ Oct 27, 2019 at 18:59
  • \$\begingroup\$ Also, you can detect the "loop forever" command by checking whether the input contains any + character, saves 1 byte because you don't need to decrement it. \$\endgroup\$ Oct 27, 2019 at 19:02
2
\$\begingroup\$

JavaScript, 62 Bytes, no infinite recursive

s=>s[1]?eval("while(s>'!');Math.random()*2**31"):'Hello World'
\$\endgroup\$
2
\$\begingroup\$

JavaScript (V8), 79 bytes

-2 bytes thanks VFDan

s=>eval([`"Hello World"`,`Math.random()*2**31|0`,`for(;;);`][s.lastIndexOf`*`])

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can shave off two bytes by turning s.lastIndexOf('*') into s.lastIndexOf`*` \$\endgroup\$
    – VFDan
    May 19, 2020 at 0:41
2
\$\begingroup\$

Pip, 39 35 bytes

IaQ'*P"Hello World"EIsQ@aPRRmE{T0a}

Try it online!

Simple if else based program, prints random integer between 0 and 1000.

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

Vyxal, 16 bytes

×=[kh|tð=[k×ʀ℅|{

Try it Online! The 5 flag is just so that you don't have to wait as long for it to timeout. Even at the default 60 second timeout, this times out on * , but you can see that it works by replacing with a smaller number.

Currently, this beats all other answers.

Explanation:

                  # Implicit input
×=[               # If input = "*":
   kh             #   Push "Hello World"
     |tð=[        # Else if last character of input = " ":
          k×      #   Push 2147483648
            ʀ     #   Range [0, 2147483648]
             ℅    #   Random integer from range
              |   #     Else:
               {  #       While True:  (Infinite loop)
                  # Implicit output
\$\endgroup\$
1
1
\$\begingroup\$

Charcoal, 30 bytes

W№θ*F⁼θ*≔Hello Worldθ∨θI‽X²¦³¹

Try it online! Link is to verbose version of code. Abuses Charcoal's default input format which splits on spaces if there is only one line, thus the random number input actually looks like three inputs. Explanation:

W№θ*

Repeat while the first input contains a *.

F⁼θ*

If the first input is a * only...

≔Hello Worldθ

... then replace it with Hello World, thus causing the loop to terminate. *+* doesn't get replaced, resulting in an infinite loop.

∨θ

If the first input is not empty then output it.

I‽X²¦³¹

But if it is empty then output a random integer in the desired range.

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

Add++, 78 bytes

z:"Hello World"
`y
xR2147483647
x:?
a:"*"
b:" * "
c:"*+*"
Ix=a,Oz
Ix=b,O
Wx=c,

Try it online!

Explanation

z:"Hello World"	; Set z = "Hello World"
`y		; Set y as the active variable
xR2147483647	; Set y to a random number between 0 and 2147483647
x:?		; Set x to the input
a:"*"		; Set a = "*"
b:" * "		; Set b = " * "
c:"*+*"		; Set c = "*+*"
Ix=a,		; If x == a then...
	Oz	;	...output z
Ix=b,		; If x == b then...
	O	;	...output y
Wx=c,		; While x == c then...
		;	...do nothing
\$\endgroup\$
1
\$\begingroup\$

Commodore BASIC (VIC-20, C64, TheC64Mini etc) - 170 tokenize BASIC bytes

 0a%=32767:goS9:b$=leF(b$,len(b$)-1):ifb$="*"tH?"hello world
 1ifb$=" * "tH?int(rN(ti)*a%)
 2ifb$="*+*"tHfOi=.to1:i=.:nE
 3end
 9b$="":fOi=.to1:geta$:i=-(a$=cH(13)):b$=b$+a$:?a$;:nE:reT

I think to do this more accurately, I'll have to delve into the weird world of 6502 assembly language, but this is a first draft.

First point, the INPUT keyword in Commodore BASIC ignores white spaces, so the sub-routine at line 9 is a quick-and-dirty way to accept keyboard entries including spaces.

Second point, Commodore BASIC integers have a range of 16-bit signed, so -32768 to +32767 source - so I've kept the random number generated to 0 - 32767 inclusive

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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