18
\$\begingroup\$

Challenge

Factory workers are usually very hard-working. However, their work is now being commonly replaced with machines.

You have to write a program that takes a number as input. It will print out a factory of 10 workers 10 times. Every time, each worker has a 1/input chance of being 'fired' and replaced by a machine.

Input

An integer, coming from STDIN or a function call.

Output

10 cases of the factory, each one with usually more workers fired.

Output format - how to print a factory

A factory looks like this:

|0000000000| or |0000011001|

A pipe represents the walls, a 0 represents a worker, and a 1 represents a machine, so the first print of the factory will always be |0000000000|.

Example

Input: 10

Output:

|0000000000| //always start off with this
|0000000010| //a 1/10 chance means that this worker lost his job
|0000010010|
|0010010010|
|1010010010|
|1010110010|
|1010110011|
|1010111011|
|1010111111|
|1110111111|


Input: 5

Output:

|0000000000| //always start here
|0000001001| //a 1/5 chance means that 2 workers got fired
|1000101001|
|1000101111|
|1101101111|
|1111111111| //after achieving all machinery, the machines continue to be printed
|1111111111|
|1111111111|
|1111111111|
|1111111111|

NOTE

The number of workers fired is RANDOM - in my examples for 1/5 chance there would always be 2 workers fired but your program has to do this randomly - sometimes 1 and sometimes 3 - they just have 1/5 chance of being fired.

\$\endgroup\$
  • 1
    \$\begingroup\$ It doesn't matter - the worker can be considered very lucky ;) \$\endgroup\$ – lolad Mar 21 '18 at 19:04
  • 2
    \$\begingroup\$ Does an input of 10 mean that each worker has a 1/10 chance of losing their job each time, or that 1/10th of the workers will be fired each time? \$\endgroup\$ – 12Me21 Mar 21 '18 at 19:06
  • 1
    \$\begingroup\$ The former as specified in the NOTE (sometimes 1 sometimes 3) \$\endgroup\$ – Weijun Zhou Mar 21 '18 at 19:07
  • 1
    \$\begingroup\$ @12Me21 it means each worker has a 1/10 chance of losing their job, NOT the latter. \$\endgroup\$ – lolad Mar 21 '18 at 19:22
  • 6
    \$\begingroup\$ @Uriel No, I'm mean =) \$\endgroup\$ – lolad Mar 21 '18 at 19:22

25 Answers 25

7
\$\begingroup\$

Japt -R, 22 21 20 19 18 bytes

AÆP=®|!UöêAçTÃû|C

Try it


Explanation

AÆP=®|!UöêAçTÃû|C     :Implicit input of integer U
A                      :10
 Æ                     :Map the range [0,A)
  P=                   :  Assign to P (initially the empty string)
    ®                  :    Map P
     |                 :      Bitwise OR with
      !                :      The negation of
       Uö              :      A random integer in the range [0,U)
         Ã             :    End Map
          ª            :    OR, for the first element when the above will still be an empty string (falsey)
           Aç          :    Ten times repeat
             T         :      Zero
              Ã        :End Map
               û|      :Centre pad each element with "|"
                 C     :  To length 12
                       :Implicitly join with newlines and output
\$\endgroup\$
7
\$\begingroup\$

R, 92 89 bytes

cat(t(cbind("|",apply("[<-"(matrix(runif(100)<1/scan(),10),1,,0),2,cummax),"|
")),sep="")

Try it online!

Ungolfed:

m <- matrix(runif(100)<1/n,10,10)   #10x10 matrix of TRUE with probability 1/n
                                    #and FALSE with probability 1-1/n
m[1,] <- 0                          #set first row to 0
m <- apply(m,2,cummax)              #take the column cumulative maxima
m <- cbind("|",m,"|\n")             #put "|" as the first and last columns
m <- t(m)                           #transpose m for the write function
cat(m,sep="")                       #print m to stdout, separated by ""

\$\endgroup\$
  • 1
    \$\begingroup\$ I spent quite a bit of time trying to beat 92 this afternoon using various implementations of replicate and for loops without success, however but with your tips for Do-while loops, I have finally just realized how abusable { potentially this. I have extended this abuse to an example with for() for a 92 bytes solution. Maybe you already realized the implications of { abuse when you wrote that tip post, but I have realized just now. tio.run/##K/r/… \$\endgroup\$ – Vlo Mar 21 '18 at 22:02
  • \$\begingroup\$ @Vlo Is that the right TIO link? Still, looks like you've beaten me with a function! I golfed you down to 82 bytes \$\endgroup\$ – Giuseppe Mar 21 '18 at 22:09
  • \$\begingroup\$ I should start an R chatroom... I think "for" is almost always no better than for, and it's occasionally worse! \$\endgroup\$ – Giuseppe Mar 21 '18 at 22:13
  • \$\begingroup\$ Haha, of course pmax can be used. Here is my earlier point about the { operator. tio.run/##PZDBasNADETv@xVDfKhNjcGHXAKl5OJTT2l/… \$\endgroup\$ – Vlo Mar 22 '18 at 2:12
6
\$\begingroup\$

JavaScript (ES6), 84 bytes

n=>[...s=2e9+''].map(j=>`|${s=s.replace(/./g,i=>i&1|Math.random()*n<!+j)}|`).join`
`

Try it online!


Recursive version, 88 bytes

n=>(g=k=>k?`|${s=s.replace(/./g,i=>i%5|Math.random()*n<(s!=k))}|
`+g(k>>3):'')(s=5e9+'')

Try it online!

How?

We start with k = s = '5000000000'.

At each iteration:

  • We coerce each character i of s to a number, compute i modulo 5 -- so that the leading 5 is treated like a 0 -- and randomly perform a bitwise OR with 1 with the expected probability 1/n, except on the first iteration.

  • The counter k is right-shifted by 3 bits. We stop the recursion as soon as k = 0, which gives 10 iterations.

    It is important to note that 5000000000 is slightly larger than a 32-bit integer, so it is implicitly converted to 5000000000 & 0xFFFFFFFF = 705032704 just before the first bitwise shift occurs. Hence the following steps:

     step | k
    ------+-----------
       0  | 705032704
       1  | 88129088
       2  | 11016136
       3  | 1377017
       4  | 172127
       5  | 21515
       6  | 2689
       7  | 336
       8  | 42
       9  | 5
      10  | 0
    
\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog), 37 bytes

⎕{⎕←' '~⍨⍕'|'⍵'|'⋄×⍵+⍺=?10⍴⍺}⍣10⊢10⍴0

Try it online!

How?

10⍴0 - start with 10 zeros.

⎕←' '~⍨⍕'|'⍵'|' - every time print the formatted array,

?10⍴⍺ - generate a random array with values ranging 1 to input,

⍺= - element-wise comparison with the input. should mark 1 / input of the elements, giving each a 1 / input every time,

⍵+ - add to the array,

× - signum. zero stays zero, anything greater than one comes back to one.

⍣10 - repeat 10 times.

\$\endgroup\$
  • \$\begingroup\$ Confused I am, +1 =) \$\endgroup\$ – lolad Mar 21 '18 at 19:24
3
\$\begingroup\$

Retina, 30 bytes

.+
|¶10*$(*0¶
.-10+0\%0<@`\d
1

Try it online!

I'm having lots of fun with randomness in Retina^^

Explanation

The first stage sets up the string we will be working with:

.+
|¶10*$(*0¶

It replaces the whole input with |, a newline, and then 10 lines containing as many 0s as the input says. The first character on each line will represent a worker of the factory.

The following stage means:

.                     Disable automatic printing at the end of the program
 -10+                 Do the following exactly 10 times:
       %                For each line:
        0<                Print the first character, then
          @`\d            Pick a random digit and
1 (on the next line)      replace it with a 1
     0\                 Then print the first character of the whole string
                        followed by a newline

The first line of the working string contains only a |, which will be the first character printed by every iteration of the loop (being the first character of the first line), and will be also printed at the end of every iteration (being the first character of the whole string). The replacement will never have any effect on this line because it doesn't contain any digit.

Each other line contains n digits, so there is a 1 in n chance to turn the first character of the line (which is the only meaningful one) into a 1.

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

PowerShell, 82 80 69 bytes

param($x)($a=,0*10)|%{"|$(-join$a)|";$a=$a|%{$_-bor!(Random -ma $x)}}

Try it online!

Takes input $x. Creates an array of all zeros, saves that into $a and then loops that many times. Conveniently, the factory is just as wide as it is iterations worth. Each iteration, we output our current factory with "|$(-join$a)|", then loop through each element of $a.

Inside we're selecting the current element $_ that has been -binary-ored with either 1 based on the Random chance based on input $x. For example, for input 10, Get-Random -max 10 will range between 0 and 9 and be 0 approximately 1/10th of the time. Thus with a !(...) wrapping the Random, we'll get a 1 approximately 1/input amount of the time, and the other 1-1/input amount of the time we'll get $_. Yes, this sometimes means that we're overwriting a 1 with another 1, but that's fine.

That resulting array is then stored back into $a for the next go-round. All of the resulting strings are left on the pipeline, and the implicit Write-Output at program completion gives us newlines for free.

-2 bytes thanks to Veskah.
-11 bytes thanks to ASCII-only.

\$\endgroup\$
  • \$\begingroup\$ 80 Bytes? \$\endgroup\$ – Veskah Feb 21 at 3:39
  • \$\begingroup\$ @Veskah Yes, that will work nicely. Thanks! \$\endgroup\$ – AdmBorkBork Feb 21 at 15:57
  • \$\begingroup\$ 78? \$\endgroup\$ – ASCII-only Feb 22 at 12:55
  • \$\begingroup\$ 69???? \$\endgroup\$ – ASCII-only Feb 22 at 13:06
  • \$\begingroup\$ @ASCII-only Oh sure, why are we re-indexing into $a when we're already looping through it? lol And that's a clever trick with the -bor. Thanks! \$\endgroup\$ – AdmBorkBork Feb 22 at 13:34
2
\$\begingroup\$

Perl 6, 58 bytes

{say "|$_|" for 0 x 10,|[\~|] ([~] +(1>$_*rand)xx 10)xx 9}

Try it online!

+(1 > $_ * rand) generates a single bit with the required frequency of 1s. xx 10 replicates that expression ten times to produce a single factory instance as a list of bits, and [~] joins that list into a single string. xx 9 replicates that factory-string-generating expression nine times, and then [\~|] does a triangular reduction (which some other languages call a "scan") with the stringwise or operator ~|, so that a worker fired in an earlier iteration remains fired in later ones.

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice. You can remove the space before for. Beats the number-based solution {say "|{.join}|"for [\<<+|>>] (?$++X*!<<^$_).roll(10)xx 10} I was working on by two bytes. With [\Z+|] it would be 56 bytes but this doesn't work for some reason. \$\endgroup\$ – nwellnhof Mar 21 '18 at 21:11
  • 1
    \$\begingroup\$ You can also replace $_*rand with .rand. The way I interpret the rules, returning a list of "factory" strings should also be OK. \$\endgroup\$ – nwellnhof Mar 21 '18 at 21:41
2
\$\begingroup\$

Python 2, 104 103 bytes

from random import*
x='0'*10;i=input()
exec"print'|%s|'%''.join(x);x=[choice('1'+n*~-i)for n in x];"*10

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Sorry, you need to format the output \$\endgroup\$ – lolad Mar 21 '18 at 19:15
  • \$\begingroup\$ @lolad fixed c: \$\endgroup\$ – Rod Mar 21 '18 at 19:19
  • \$\begingroup\$ well done, +1 =) \$\endgroup\$ – lolad Mar 21 '18 at 19:21
2
\$\begingroup\$

Jelly, 22 bytes

ẋ⁵ẋ€⁵¬1¦X€€=1o\j@€⁾||Y

A full program accepting the integer as a command line input and printing the output to STDOUT.
(As a monadic link it returns a list of characters and integers.)

Try it online!

How?

Effectively decides at each stage if each worker (including any machines) lose their job (with a one in N chance), however machines are replaced by machines (using logical-OR).

ẋ⁵ẋ€⁵¬1¦X€€=1o\j@€⁾||Y - Main link: integer, N
 ⁵                     - literal ten
ẋ                      - repeat -> [N,N,N,N,N,N,N,N,N,N]
    ⁵                  - literal ten
  ẋ€                   - repeat €ach -> [[N,N,N,N,N,N,N,N,N,N],...,[N,N,N,N,N,N,N,N,N,N]]
       ¦               - sparse application...
      1                - ...to indices: [1]
     ¬                 - ...do: logical-NOT -> [[0,0,0,0,0,0,0,0,0,0],[N,N,...],...]
        X€€            - random integer for €ach for €ach
                       -   (0s stay as 0s; Ns become random integers in [1,N])
           =1          - equals one? (vectorises)
             o\        - cumulative reduce with logical-OR
                  ⁾||  - literal list of characters = ['|','|']
               j@€     - join with sw@pped arguments for €ach
                     Y - join with line feeds
                       - implicit print
\$\endgroup\$
2
\$\begingroup\$

MATL, 26 bytes

'|'it10th&Yr=0lY(Y>48+y&Yc

Try it online!

(Long) explanation

Example stack contents are shown along the way. At each step, stack contents are shown bottom to top.

'|'    % Push this character
       % STACK: '|'
it     % Take input. Duplicate
       % STACK: '|'
                5
                5
10th   % Push [10 10]
       % STACK: '|'
                5
                5
                [10 10]
&Yr    % Random 10×10 matrix of integers from 1 to input number
       % STACK: '|'
                 5
                 [4 5 4 4 5 5 2 1 2 3
                  3 4 3 3 1 4 4 3 1 5
                  5 1 4 5 4 4 5 2 3 2
                  3 4 5 2 1 3 2 5 3 4
                  4 1 2 2 4 1 1 5 1 1
                  4 5 3 1 5 3 5 2 4 1
                  2 1 4 3 3 1 3 5 3 5
                  1 2 2 1 2 2 4 3 5 3
                  4 5 4 1 2 2 5 3 2 4
                  4 1 2 5 5 5 4 3 5 1]
=      % Is equal? Element-wise
       % STACK: '|'
                [0 1 0 0 1 1 0 0 0 0
                 0 0 0 0 0 0 0 0 0 1
                 1 0 0 1 0 0 1 0 0 0
                 0 0 1 0 0 0 0 1 0 0
                 0 0 0 0 0 0 0 1 0 0
                 0 1 0 0 1 0 1 0 0 0
                 0 0 0 0 0 0 0 1 0 1
                 0 0 0 0 0 0 0 0 1 0
                 0 1 0 0 0 0 1 0 0 0
                 0 0 0 1 1 1 0 0 1 0]
0lY(   % Write 0 in the first row
       % STACK: '|'
                [0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 1
                 1 0 0 1 0 0 1 0 0 0
                 0 0 1 0 0 0 0 1 0 0
                 0 0 0 0 0 0 0 1 0 0
                 0 1 0 0 1 0 1 0 0 0
                 0 0 0 0 0 0 0 1 0 1
                 0 0 0 0 0 0 0 0 1 0
                 0 1 0 0 0 0 1 0 0 0
                 0 0 0 1 1 1 0 0 1 0]
Y>     % Cumulative maximum down each column
       % STACK: '|'
                [0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 1
                 1 0 0 1 0 0 1 0 0 1
                 1 0 1 1 0 0 1 1 0 1
                 1 0 1 1 0 0 1 1 0 1
                 1 1 1 1 1 0 1 1 0 1
                 1 1 1 1 1 0 1 1 0 1
                 1 1 1 1 1 0 1 1 1 1
                 1 1 1 1 1 0 1 1 1 1
                 1 1 1 1 1 1 1 1 1 1]
48+    % Add 48, element-wise. This transforms each number into the
       % ASCII code of its character representation
       % STACK: '|'
                [48 48 48 48 48 48 48 48 48 48
                 48 48 48 48 48 48 48 48 48 49
                 49 48 48 49 48 48 49 48 48 49
                 49 48 49 49 48 48 49 49 48 49
                 49 48 49 49 48 48 49 49 48 49
                 49 49 49 49 49 48 49 49 48 49
                 49 49 49 49 49 48 49 49 48 49
                 49 49 49 49 49 48 49 49 49 49
                 49 49 49 49 49 48 49 49 49 49
                 49 49 49 49 49 49 49 49 49 49]
y      % Duplicate from below
       % STACK: '|'
                [48 48 48 48 48 48 48 48 48 48
                 48 48 48 48 48 48 48 48 48 49
                 49 48 48 49 48 48 49 48 48 49
                 49 48 49 49 48 48 49 49 48 49
                 49 48 49 49 48 48 49 49 48 49
                 49 49 49 49 49 48 49 49 48 49
                 49 49 49 49 49 48 49 49 48 49
                 49 49 49 49 49 48 49 49 49 49
                 49 49 49 49 49 48 49 49 49 49
                 49 49 49 49 49 49 49 49 49 49]
                 '|'
&Yc    % Horizontally concatenate all stack contents as char arrays.
       % 1-row arrays are implicitly replicated along first dimension
       % STACK: ['|0000000000|'
                 '|0000000001|'
                 '|1001001001|'
                 '|1011001101|'
                 '|1011001101|'
                 '|1111101101|'
                 '|1111101101|'
                 '|1111101111|'
                 '|1111101111|'
                 '|1111111111|']
       % Implicitly display
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 105 93 90 bytes

x=>w.map(a=>(`|${w=w.map(j=>j|Math.random()<1/x),w.join``}|
`)).join``,w=Array(10).fill(0)

Try it online!

+2 bytes for putting the array inside the function, thanks to @Shaggy for pointing that out

(x,w=Array(10).fill(0))=>w.map(a=>(`|${w=w.map(j=>j|Math.random()<1/x),w.join``}|
`)).join``

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Note that functions need to be reusable here, so w will need to be declared within your function. \$\endgroup\$ – Shaggy Mar 21 '18 at 21:43
  • \$\begingroup\$ @Shaggy, thanks I edited it. unfortunately it added 2 bytes \$\endgroup\$ – Joost_96 Mar 22 '18 at 6:36
2
\$\begingroup\$

C (gcc), 110 106 bytes

-4 bytes from @ceilingcat

char l[]="|0000000000|",i,j;f(n){for(srand(time(i=0));i++<10;)for(j=!puts(l);j++<10;)rand()%n||(l[j]=49);}

Try it online!

Iterates through a list of characters for each round of replacements.

Ungolfed:

f(n)
{
    //start with a fresh factory
    char l[]="|0000000000|";

    //init rng
    srand(time(0));

    //print 11 lines
    for(int i = 0; i < 11; i++)
    {
        //print current factory
        puts(l);

        //iterate through the workers. start at index 1 to skip the '|'
        for(int j = 1; j < 11; j++)
        {
            //the expression (rand()%n) has n possible values in the range [0,n-1],
            //so the value 0 has 1/n chance of being the result and thus the worker
            //has 1/n chance of being replaced
            if(rand()%n == 0)
            {
                l[j] = '1';
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I think you print 1 too many factories. The example shows 10, but your TIO link shows 11. \$\endgroup\$ – Brian J Mar 22 '18 at 15:54
  • \$\begingroup\$ Hm you're right. I changed it to 11 after reading a comment on someone else's solution telling them it's supposed to be 11, but I never actually verified the example from the challenge. Thanks \$\endgroup\$ – vazt Mar 22 '18 at 18:39
1
\$\begingroup\$

SmileBASIC, 75 bytes

INPUT N
FOR J=0TO 9?"|";BIN$(F,10);"|
FOR I=0TO 9F=F OR!RND(N)<<I
NEXT
NEXT
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 44 bytes

#!/usr/bin/perl -a
say"|$_|",s/0/rand"@F"<1|0/eg<0for(0 x10)x10

Try it online!

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

05AB1E, 22 bytes

TÅ0TFDJ'|.ø=sITи€L€ΩΘ~

Try it online!

There should be some more room for golfing.

  • TÅ0 — Push a list of 10 zeroes.
  • TF... — Do this 10 times:
    • DJ — Duplicate the current item and join it.
    • '|.ø= — Surround it with two |s and print to STDOUT.
    • ITи — Repeat the input 10 times.
    • €L€Ω — And for each occurrence, get a random element of [1 ... N]. (There might be a built-in for this which I haven’t seen yet)
    • Θ — Push 05AB1E truthified™. For each, check if it equals 1.
    • s...~ — Logical OR the result by the current item.
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 83 bytes

f=(n,t=9,s=`|0000000000|
`)=>t?s+f(n,t-1,s.replace(/0/g,v=>+(Math.random()<1/n))):s

f=(n,t=9,s=`|0000000000|
`)=>t?s+f(n,t-1,s.replace(/0/g,v=>+(Math.random()<1/n))):s
<p><input id=i value=10 /><button id=t onclick="o.textContent=f(+i.value)">Run</button><p><pre id=o></pre>

\$\endgroup\$
  • \$\begingroup\$ 80 bytes \$\endgroup\$ – Shaggy Mar 22 '18 at 12:55
1
\$\begingroup\$

Java 10, 153 152 131 bytes

n->{for(int j=10,a[]=new int[j],i;j-->0;){var s="|";for(i=0;i<10;a[i++]|=Math.random()*n<1?1:0)s+=a[i];System.out.println(s+"|");}}

-18 bytes thanks to @OlivierGrégoire, and -3 more bytes by converting Java 8 to Java 10.

Explanation:

Try it online.

n->{                             // Method with integer parameter and no return-type
  for(int j=10,a[]=new int[j],i; //  Create the array with 10 workers (0s)
      j-->0;){                   //  Loop 10 times

    var s="|";                   //  Start a String with a wall
    for(i=0;i<10;                //  Loop `i` from 0 to 10 (exclusive)
        a[i++]                   //    After every iteration, change the current item to:
         |=                      //     If it's a 0:
           Math.random()*n<1?    //      If the random value [0:1) is below 1/n
            1                    //       Change the worker to a machine
           :                     //      Else:
            0;                   //       It remains a worker
                                 //     Else (it already is a machine)
                                 //      It remains a machine
      s+=a[i];                   //   Append the current item to the String
    System.out.println(s+"|");}} //   Print the String, with a trailing wall and new-line
\$\endgroup\$
  • 1
    \$\begingroup\$ 131 bytes. Replace var by String for Java 9 and below and for an extra 3 bytes. I basically merged the two loops you have. \$\endgroup\$ – Olivier Grégoire Mar 22 '18 at 14:08
1
\$\begingroup\$

Charcoal, 30 29 27 bytes

⊞υ×χ0Fχ⊞υ⭆§υ±¹‽⁺1×κ⊖θEυ⪫||ι

Try it online! Link is to verbose version of code. Explanation:

⊞υ×χ0

Push an string of 10 0s to the empty list u.

Fχ

Repeat the next command 10 times.

⊞υ⭆§υ±¹‽⁺1×κ⊖θ

For each character of the last string, repeat it n-1 times, add a 1, and pick a random character from the string. This gives a 1/n chance of changing the character to a 1. The result is pushed to u.

Eυ⪫||ι

Map over the list of strings, surrounding each with |, then implicitly print each on its own line.

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

Python 3, 132 bytes

from random import*
w,p,c=[0]*10,'|',1/int(input())
for _ in w:print(p+''.join(map(str,w))+p);w=list(map(lambda x:x|(random()<c),w))

Try it online!

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

APL+WIN, 30 40 35 bytes

Missed the bit about no spaces ;( - fixed & thanks to Uriel for -3 bytes

Prompts for screen input of number

'|',(⍕0⍪×+⍀n=?9 10⍴n←⎕)[;2×⍳10],'|'

Explanation similar to that of Uriel:

?9 10⍴n create a 9x10 matrix of numbers selected at random between 1 and number

×+⍀n= compare elements of matrix to number, cumulatively sum columns and signum

[;2×⍳10] take non-space columns

'|',(⍕0⍪.......,'|' concatenate a row of zeros and front and back columns of |
\$\endgroup\$
  • \$\begingroup\$ 9 10⍴?90⍴5?9 10⍴5. You also need to eliminate spaces \$\endgroup\$ – Uriel Mar 21 '18 at 21:14
  • \$\begingroup\$ @Uriel thanks for the -3 bytes and I had missed the no space rule. \$\endgroup\$ – Graham Mar 21 '18 at 21:33
0
\$\begingroup\$

VBA, 144 bytes

Sub f(i)
Dim w(9) As Long
For x = 0 To 9
s = "|"
For y = 0 To 9
s = s & w(y)
If i * Rnd() < 1 Then w(y) = 1
Next
Debug.Print s; "|"
Next
End Sub

Indented for easier reading:

Sub f(i)
    Dim w(9) As Long
    For x = 0 To 9
        s = "|"
        For y = 0 To 9
            s = s & w(y)
            If i * Rnd() < 1 Then w(y) = 1
        Next
        Debug.Print s; "|"
    Next
End Sub

Takes advantage of 2 points: VBA Arrays will default to Base 0 (so w(9) is the same as w(0 to 9)) and creating the Array as a Long will automatically initialise it to 0.

(Annoyingly, 20 bytes are auto-formatting that VBA adds in but not actually required - 19 spaces and one semi-colon)

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

I don't see answer for Ruby yet, so:

Ruby, 92 bytes

n=gets.to_i
z='0'*x=10
x.times do
puts'|'+z+'|'
x.times do|s|
z[s]='1' if rand(n)==0
end
end

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Save a byte by using rand(n)<1 instead of rand(n)==0, and save a few by using {..} instead of do..end, e.g. x.times{puts'|'+z...} \$\endgroup\$ – Piccolo Aug 8 '18 at 1:08
0
\$\begingroup\$

Ruby, 67 bytes

->n{f=[0]*10;10.times{p"|#{f.join}|";f.map!{|l|rand>(1.0/n)?l:?1}}}

I think I've cheated a few things here. First of all, this function prints the output with quotes around each line, e.g.:

pry(main)> ->n{f=[0]*10;10.times{p"|#{f.join}|";f.map!{|l|rand>(1.0/n)?l:?1}}}[5]
"|0000000000|"
"|0100100000|"
"|0100100000|"
"|0100100000|"
"|0100100100|"
"|0100110100|"
"|0101110100|"
"|0111110100|"
"|1111110110|"
"|1111110110|"
=> 10

If this is unacceptable (given that this is , that is probably the case), here is a solution that prints without quotes for 70 bytes:

->n{f=[0]*10;10.times{puts"|#{f.join}|";f.map!{|l|rand>(1.0/n)?l:?1}}}

Explanation:

->n{                                                              } # defines anonymous lambda
    f=[0]*10;                                                       # initializes f to [0,0,0,...] (10)
             10.times{                                           }  # main loop
                      p"|#{f.join}|";                               # joins f and prints | before and after
                                     f.map!{|l|                 }   # maps each element of f
                                               rand>(1.0/n)? :      # if rand is greater than 1.0/n
                                                            l ?1    # keep current element or replace with '1'
\$\endgroup\$
0
\$\begingroup\$

PHP, 71 70 bytes

merging loops saved 5 bytes (again):

for(;$k<91;$v|=!rand(0,$argn-1)<<$k++%10)$k%10||printf("|%010b|
",$v);

Run as pipe with -nR or try it online.


edit 1: fixed format and first output (no change in byte count thanks to additional golfing)
edit 2: Golfed one more byte: After the last printing, there´s no more need for firing anyone.

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

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

n=>{var r=new Random();var a=new int[10];for(int i=0;i<100;a[i%10]|=r.Next(n)<1?1:0)if(i++%10<1)Write("|"+string.Concat(a)+"|\n");}

Hey, at least I tied with Java.

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.