11
\$\begingroup\$

Challenge:

In a hypothetical scenario, the countdown timer for a race has random intervals between the counts, to prevent premature starting, e.g.

3 (0.82 seconds pass), 2 (0.67 seconds pass), 1

Input:

nothing


Output:

Write a program (or function) that prints the 3 numbers with a random time interval from 0.50 seconds to 1 second between each count.


Note:

  • The program must output each number (3 , 2 , 1) with the random (any number between 0.50 and 1 to the hundredths; no hard-coding) time interval between each. The precision of the random interval must go out to the hundreds (e.g: 0.52). You are not required to output the interval, only the count.
  • As @JoKing clarified, I mean uniformly random (you can use the pseudo-random generator of your language.
  • As many people have clarified, I really mean any 2-decimal number between 0.5 and 1. (0.50, 0.51, etc, all the way to 0.98, 0.99, 1)

This is , so the program with the lowest byte count wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ Hi LordColus, and welcome to PPCG! This seems like a good first challenge. For future challenges, we recommend going through the sandbox first to iron out all the details. \$\endgroup\$ – Mnemonic May 1 '18 at 3:05
  • 1
    \$\begingroup\$ @LordColus : I improved the original statement and couple of other edits , take a look and approve if you want. \$\endgroup\$ – Muhammad Salman May 1 '18 at 14:43
  • 1
    \$\begingroup\$ As I said in a previous comment that has been deleted, specifying "uniformly random" is fine if you are not too stringent with precision. As it stands now, the pause times must be uniform with precision of two decimals (or is it at least two decimals?). Does that mean the distribution should be uniform on the set 0.5, 0.51, 0.52, ..., 1, or can it be any floating point (possibly with more than two decimals) beween 0.5 and 1? \$\endgroup\$ – Luis Mendo May 2 '18 at 10:07
  • 2
    \$\begingroup\$ Does my most recent edit clear it up? \$\endgroup\$ – LordColus May 2 '18 at 12:02
  • 6
    \$\begingroup\$ @mbomb007 Same... why did this get closed again? It's basically count from 3 to 1 with two .50-1.00 second waits in between. It's really not complicated. \$\endgroup\$ – Magic Octopus Urn May 2 '18 at 17:01

11 Answers 11

5
\$\begingroup\$

05AB1E, 12 bytes

3LRε=₄D;ŸΩ.W

Try it online!


3LR          # Push [3,2,1]
   ε         # For each...
    =        # Print it.
     ₄       # Push 1000.
      D      # Duplicate top (1000).
       ;     # Divided by 2 (500).
        Ÿ    # Range from b to a ([1000 .. 500]).
         Ω   # Random pick.
          .W # Wait X ms.

Try it with debug enabled: Try it online!

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

SmileBASIC, 64 62 bytes

?3M?2M?1DEF M
M=MILLISEC
WHILE MILLISEC<M+500+RND(501)WEND
END

Unfortunately I can't use WAIT since that only supports intervals of 1/60 of a second (anything less isn't normally useful since input/output only update once per frame)

This requires adjustment depending on the speed of the system it's running on, so it might not be valid (46 bytes):

?3M?2M?1DEF M
FOR I=-66E4-RND(66E4)TO.NEXT
END

Invalid WAIT version (36 bytes):

?3WAIT 30+RND(30)?2WAIT 30+RND(30)?1
\$\endgroup\$
2
\$\begingroup\$

R, 46 44 bytes

for an actual countdown:

for(i in 3:1){cat(i)
Sys.sleep(runif(1,.5))}

Try it online!

printing interval as I initially misunderstood the challenge (46 bytes) Thanks Giuseppe for saving 2 chars.

for(i in 3:1)cat(i,format(runif(1,.5),,2)," ")

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I believe runif() by default has the left and right endpoints as 0 and 1 respectively, so runif(1,.5) should work the same for -2 bytes in both. \$\endgroup\$ – Giuseppe May 1 '18 at 19:45
  • \$\begingroup\$ Good catch thanks @Giuseppe. \$\endgroup\$ – JayCe May 1 '18 at 20:01
2
\$\begingroup\$

Python 2, 58 bytes

from time import*
for a in'321':print a;sleep(1-time()%.5)

Try it online!

I created a very simple random number generator that takes the seed time (as many people do).


Improvements

\$\endgroup\$
  • \$\begingroup\$ 1-time()%.5 should do the trick. (You need [3,2,1] by the way) \$\endgroup\$ – Jonathan Allan May 1 '18 at 20:38
  • \$\begingroup\$ Also for a in'321' saves another two \$\endgroup\$ – Jonathan Allan May 1 '18 at 20:50
  • \$\begingroup\$ @JonathanAllan Very good point, updated. \$\endgroup\$ – Neil May 1 '18 at 22:21
  • \$\begingroup\$ This sleeps once before the countdown too. I think you need the print statement before the sleep. \$\endgroup\$ – Magic Octopus Urn May 2 '18 at 17:05
  • \$\begingroup\$ @MagicOctopusUrn Agreed, updated. \$\endgroup\$ – Neil May 3 '18 at 3:56
1
\$\begingroup\$

APL+WIN, 37 bytes

3⋄r←⎕dl ↑n←.49+.01×2?51⋄2⋄r←⎕dl 1↓n⋄1
\$\endgroup\$
1
\$\begingroup\$

Java 8, 86 bytes

v->{for(int i=0;++i<4;Thread.sleep((int)(Math.random()*500+500)))System.out.print(i);}

Prints without delimiter. If that is not allowed it's +2 bytes by changing print to println (new-line delimiter).

Try it online.
Prove the intervals are in the correct range of [500, 1000) ms.

Explanation:

v->{                        // Method with empty unused parameter and no return-type
  for(int i=0;++i<4;        //  Loop in range [1,4)
      Thread.sleep((int)(Math.random()*500+500)))
                            //    After every iteration: sleep for [500, 1000) ms randomly
     System.out.print(i);}  //   Print the current number
\$\endgroup\$
1
\$\begingroup\$

Python 3, 122 bytes

import random as r,time
def w():time.sleep(abs(r.random()-.5)+.5)
print(3,end="");w();print(", 2",end="");w();print(", 1")

Try it online!

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

JavaScript (Node.js), 75 65 60 bytes

  • thanks to @Shaggy for reducing by 10 bytes
  • thanks to @Kevin Cruijssen for reducing by 5 bytes
f=(i=3)=>i&&setTimeout(f,Math.random()*500+500,i-1,alert(i))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 55 bytes \$\endgroup\$ – Shaggy May 1 '18 at 21:42
  • 1
    \$\begingroup\$ @Shaggy will it really be random if you're using Date ? (for the second time the random occured) \$\endgroup\$ – DanielIndie May 2 '18 at 3:27
  • 1
    \$\begingroup\$ Why *1000%500+500? You can just use *500+500. \$\endgroup\$ – Kevin Cruijssen May 2 '18 at 7:08
  • \$\begingroup\$ With the updates to the spec, it probably won't be random enough but it might be worth asking for clarification. \$\endgroup\$ – Shaggy May 2 '18 at 9:27
1
\$\begingroup\$

Perl 5, 39 bytes

Gradually tweaked down to 39 bytes, thanks to @jonathan-allan + @xcali.

say-$_+select$a,$a,$a,1-rand.5for-3..-1

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ would 1-rand(.5) work? \$\endgroup\$ – Jonathan Allan May 1 '18 at 20:56
  • \$\begingroup\$ Good idea. Can then use 1-rand.5 too. \$\endgroup\$ – steve May 1 '18 at 21:00
  • 1
    \$\begingroup\$ Cut four more bytes by removing the parens and changing the counter to be negative. Try it online! \$\endgroup\$ – Xcali May 2 '18 at 17:07
1
\$\begingroup\$

Chip -wingjj, 33 bytes

0123456e7f s
???????p*9S!ZZZtaABb

Try it online!

In Chip, we cannot wait for exactly 1/100 of a second, but we can wait for 1/256 of a second, so we use that here.

p, when asked, will pause execution for the stack head (one byte) * 1/256 seconds. On each cycle, we always set the high bit of the stack (128/256) and set all other stack bits randomly (with the ?'s). This gives an even distribution between 0.50 and 1.00 seconds.

Some of the args, -w and -gjj, specify that the input, instead of using stdin, should be a countdown from 0xFF to 0x00 (then wrapping). We use this to provide the low two bits for counting down. All other output bits remain constant (at the value corresponding to ASCII 0).

Finally, once we are done, we terminate the program with t, preventing a pause after the last number.

\$\endgroup\$
0
\$\begingroup\$

[GAWK], 57 bytes

BEGIN{while(a++<2){print 4-a;sleep(rand()/2+0.5)}print 1}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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