Factory workers

18

1

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.

lolad

Posted 2018-03-21T18:50:19.610

Reputation: 754

The input is always >1? what happens if you still have a worker after 10 iterations ? – Rod – 2018-03-21T18:59:21.487

1It doesn't matter - the worker can be considered very lucky ;) – lolad – 2018-03-21T19:04:48.097

2Does 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? – 12Me21 – 2018-03-21T19:06:29.483

1The former as specified in the NOTE (sometimes 1 sometimes 3) – Weijun Zhou – 2018-03-21T19:07:19.367

can we have spaces between the workers, like | 0 0 0 0 0 0 0 0 0 0 |? – Uriel – 2018-03-21T19:11:44.867

1@12Me21 it means each worker has a 1/10 chance of losing their job, NOT the latter. – lolad – 2018-03-21T19:22:24.003

6@Uriel No, I'm mean =) – lolad – 2018-03-21T19:22:53.087

@Arnauld it seems like it was simply a mistake, and I was the only one who took it as correct :) – Jonathan Allan – 2018-03-21T21:19:30.593

Shouldthe 1/x chance per worker or per employed worker? – Pureferret – 2018-03-22T09:02:55.243

1@Pureferret It doesn´t matter. Any worker already sacked could have a (x-1)/x chance of not being fired again; but he stays fired. – Titus – 2019-02-22T17:26:16.123

Answers

7

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

Shaggy

Posted 2018-03-21T18:50:19.610

Reputation: 24 623

7

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 ""

Giuseppe

Posted 2018-03-21T18:50:19.610

Reputation: 21 077

1

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. https://tio.run/##K/r/PyEtvyhBI1OnusK2ODkxT0PTusy2KLVAw0DH0EDT2tDKslanOjmxREOpRkmnTEephktJpzi1wFZJCaQwN7GgIKcSKldUmpeZpgHUZWOoX6GpbVCr@d/0PwA

– Vlo – 2018-03-21T22:02:37.560

@Vlo Is that the right TIO link? Still, looks like you've beaten me with a function! I golfed you down to 82 bytes

– Giuseppe – 2018-03-21T22:09:41.713

I should start an R chatroom... I think "for" is almost always no better than for, and it's occasionally worse!

– Giuseppe – 2018-03-21T22:13:09.073

6

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
    

Arnauld

Posted 2018-03-21T18:50:19.610

Reputation: 111 334

4

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.

Uriel

Posted 2018-03-21T18:50:19.610

Reputation: 11 708

Confused I am, +1 =) – lolad – 2018-03-21T19:24:11.267

3

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.

AdmBorkBork

Posted 2018-03-21T18:50:19.610

Reputation: 41 581

80 Bytes? – Veskah – 2019-02-21T03:39:31.507

@Veskah Yes, that will work nicely. Thanks! – AdmBorkBork – 2019-02-21T15:57:05.077

78? – ASCII-only – 2019-02-22T12:55:58.570

69???? – ASCII-only – 2019-02-22T13:06:10.153

@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! – AdmBorkBork – 2019-02-22T13:34:51.000

3

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.

Leo

Posted 2018-03-21T18:50:19.610

Reputation: 8 482

2

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!

Rod

Posted 2018-03-21T18:50:19.610

Reputation: 17 588

Sorry, you need to format the output – lolad – 2018-03-21T19:15:40.003

@lolad fixed c: – Rod – 2018-03-21T19:19:54.433

well done, +1 =) – lolad – 2018-03-21T19:21:26.537

2

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!

Joost K

Posted 2018-03-21T18:50:19.610

Reputation: 210

1Note that functions need to be reusable here, so w will need to be declared within your function. – Shaggy – 2018-03-21T21:43:52.300

@Shaggy, thanks I edited it. unfortunately it added 2 bytes – Joost K – 2018-03-22T06:36:38.083

2

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

Jonathan Allan

Posted 2018-03-21T18:50:19.610

Reputation: 67 804

2

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.

Sean

Posted 2018-03-21T18:50:19.610

Reputation: 4 136

1Nice. 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. – nwellnhof – 2018-03-21T21:11:12.407

1You can also replace $_*rand with .rand. The way I interpret the rules, returning a list of "factory" strings should also be OK. – nwellnhof – 2018-03-21T21:41:30.643

2

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';
            }
        }
    }
}

vazt

Posted 2018-03-21T18:50:19.610

Reputation: 311

I think you print 1 too many factories. The example shows 10, but your TIO link shows 11. – Brian J – 2018-03-22T15:54:47.453

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 – vazt – 2018-03-22T18:39:16.727

2

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

Luis Mendo

Posted 2018-03-21T18:50:19.610

Reputation: 87 464

1

SmileBASIC, 75 bytes

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

12Me21

Posted 2018-03-21T18:50:19.610

Reputation: 6 110

1

Perl 5, 44 bytes

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

Try it online!

Ton Hospel

Posted 2018-03-21T18:50:19.610

Reputation: 14 114

1

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.

Mr. Xcoder

Posted 2018-03-21T18:50:19.610

Reputation: 39 774

1

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.

Neil

Posted 2018-03-21T18:50:19.610

Reputation: 95 035

1

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>

tsh

Posted 2018-03-21T18:50:19.610

Reputation: 13 072

80 bytes – Shaggy – 2018-03-22T12:55:29.987

1

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

Kevin Cruijssen

Posted 2018-03-21T18:50:19.610

Reputation: 67 575

1131 bytes. Replace var by String for Java 9 and below and for an extra 3 bytes. I basically merged the two loops you have. – Olivier Grégoire – 2018-03-22T14:08:16.043

0

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 |

Graham

Posted 2018-03-21T18:50:19.610

Reputation: 3 184

9 10⍴?90⍴5?9 10⍴5. You also need to eliminate spaces – Uriel – 2018-03-21T21:14:24.127

@Uriel thanks for the -3 bytes and I had missed the no space rule. – Graham – 2018-03-21T21:33:06.553

0

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!

Rick Rongen

Posted 2018-03-21T18:50:19.610

Reputation: 223

0

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)

Chronocidal

Posted 2018-03-21T18:50:19.610

Reputation: 571

0

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!

Turophile

Posted 2018-03-21T18:50:19.610

Reputation: 181

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...} – Piccolo – 2018-08-08T01:08:45.637

0

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'

Piccolo

Posted 2018-03-21T18:50:19.610

Reputation: 261

0

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.

Titus

Posted 2018-03-21T18:50:19.610

Reputation: 13 814

0

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!

Embodiment of Ignorance

Posted 2018-03-21T18:50:19.610

Reputation: 7 014