Drag Race Countdown

10

0

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.

LordColus

Posted 2018-05-01T03:01:17.533

Reputation: 229

4

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.

– None – 2018-05-01T03:05:50.783

1@LordColus : I improved the original statement and couple of other edits , take a look and approve if you want. – Muhammad Salman – 2018-05-01T14:43:42.933

1As 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? – Luis Mendo – 2018-05-02T10:07:59.683

I mean that it can be any 2-digit decimal between 0.5 and 1 – LordColus – 2018-05-02T11:57:56.007

2Does my most recent edit clear it up? – LordColus – 2018-05-02T12:02:40.533

@LordColus I believe so. I voted to reopen already, so I cannot do it again. – mbomb007 – 2018-05-02T15:29:16.257

6@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. – Magic Octopus Urn – 2018-05-02T17:01:55.860

Answers

5

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!

Magic Octopus Urn

Posted 2018-05-01T03:01:17.533

Reputation: 19 422

2

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

12Me21

Posted 2018-05-01T03:01:17.533

Reputation: 6 110

2

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!

JayCe

Posted 2018-05-01T03:01:17.533

Reputation: 2 655

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. – Giuseppe – 2018-05-01T19:45:09.437

Good catch thanks @Giuseppe. – JayCe – 2018-05-01T20:01:59.793

2

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

Neil

Posted 2018-05-01T03:01:17.533

Reputation: 2 417

1-time()%.5 should do the trick. (You need [3,2,1] by the way) – Jonathan Allan – 2018-05-01T20:38:09.337

Also for a in'321' saves another two

– Jonathan Allan – 2018-05-01T20:50:47.297

@JonathanAllan Very good point, updated. – Neil – 2018-05-01T22:21:13.190

This sleeps once before the countdown too. I think you need the print statement before the sleep. – Magic Octopus Urn – 2018-05-02T17:05:33.907

@MagicOctopusUrn Agreed, updated. – Neil – 2018-05-03T03:56:13.590

1

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!

DanielIndie

Posted 2018-05-01T03:01:17.533

Reputation: 1 220

55 bytes – Shaggy – 2018-05-01T21:42:37.853

1@Shaggy will it really be random if you're using Date ? (for the second time the random occured) – DanielIndie – 2018-05-02T03:27:07.427

1Why *1000%500+500? You can just use *500+500. – Kevin Cruijssen – 2018-05-02T07:08:47.297

With the updates to the spec, it probably won't be random enough but it might be worth asking for clarification. – Shaggy – 2018-05-02T09:27:52.357

1

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!

hakr14

Posted 2018-05-01T03:01:17.533

Reputation: 1 295

6You need the imports – Jonathan Allan – 2018-05-01T20:34:42.070

On scoring imported functions – mbomb007 – 2018-05-02T15:30:31.663

Whoops! It's been fixed. – hakr14 – 2018-05-02T16:06:44.633

1

APL+WIN, 37 bytes

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

Graham

Posted 2018-05-01T03:01:17.533

Reputation: 3 184

1

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!

steve

Posted 2018-05-01T03:01:17.533

Reputation: 2 276

1would 1-rand(.5) work? – Jonathan Allan – 2018-05-01T20:56:30.647

Good idea. Can then use 1-rand.5 too. – steve – 2018-05-01T21:00:56.410

1

Cut four more bytes by removing the parens and changing the counter to be negative. Try it online!

– Xcali – 2018-05-02T17:07:42.987

1

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

Kevin Cruijssen

Posted 2018-05-01T03:01:17.533

Reputation: 67 575

1

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.

Phlarx

Posted 2018-05-01T03:01:17.533

Reputation: 1 366

0

[GAWK], 57 bytes

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

Try it online!

steve

Posted 2018-05-01T03:01:17.533

Reputation: 2 276