OK. Here goes: Is there an elegant way to generate the permutations of all the members of a set?
Almost 3 years later!
Leaner (108 characters) than my last version (113 characters).
size(530,530);for(int i=0,y=0,n=23;i<n*n;i++){int r=(int)random(2)*n,x=i%n*n;y=i/n*n;line(x+r,y,x+n-r,y+n);}
size(530, 530);
for(int i=0, y=0, n=23; i<n*n; i++)
{
int r=(int)random(2)*n, x=i%n*n;
y=i/n*n;
line(x+r, y, x+n-r, y+n);
}
Another variation at 108 characters:
size(530,530);int i=0,n=23;while(i<n*n){int r=(int)random(2)*n,x=i%n*n;int y=i++/n*n;line(x+r,y,x+n-r,y+n);}
size(530, 530);
int i=0, n=23;
while(i<n*n)
{
int r=(int)random(2)*n, x=i%n*n;
int y=i++/n*n;
line(x+r, y, x+n-r, y+n);
}
And yet another version at 104 characters!
size(530,530);int i=0,n=23;while(i<n*n){int r=(int)random(2)*n,x=i%n*n,y=i++/n*n;line(x+r,y,x+n-r,y+n);}
size(530, 530);
int i=0, n=23;
while(i<n*n)
{
int r=(int)random(2)*n, x=i%n*n, y=i++/n*n;
line(x+r, y, x+n-r, y+n);
}
I will come back in 3 years to revisit this! Maybe sooner… :)
Back…
This version has 103 characters:
size(530, 530);for(int j=0,m=23;j<m*m;){int r=(int)random(2)*m,x=j%m*m,y=j++/m*m;line(x+r,y,x+m-r,y+m);}
size(530, 530);
for(int j=0, m=23; j<m*m;)
{
int r=(int)random(2)*m, x=j%m*m, y=j++/m*m;
line(x+r, y, x+m-r, y+m);
}
:)
100 char:
size(530,530);for(int j=-1,m=23,r=0;j<530;r=random(2)>1?m:0)line(j%m*m+r,j/m*m,j%m*m+m-r,j++/m*m+m);
which is
size(530,530);
for(int j=-1, m=23, r=0; j<530; r=random(2)>1?m:0)
line(j%m*m+r, j/m*m, j%m*m+m-r, j++/m*m+m);
109 char (p5js)
setup=_=>{for(createCanvas(M=530,M),j=0,m=23;j<M;)r=random([0,m]),x=j%m*m,line(x+r,y=int(j++/m)*m,x+m-r,y+m)}
setup=_=>{
for(createCanvas(M=530,M),j=0,m=23;j<M;)
r=random([0,m]),
x=j%m*m,
line(x+r,y=int(j++/m)*m,x+m-r,y+m)
}
@micuat Nice!
99 char:
size(530,530);for(int j=0,m=23,r=0;j<m*m;r=random(2)>1?m:0)line(j%m*m+r,j/m*m,j%m*m+m-r,j++/m*m+m);
size(530, 530);
for(int j=0, m=23, r=0; j<m*m; r=random(2)>1?m:0)
line(j%m*m+r, j/m*m, j%m*m+m-r, j++/m*m+m);
You can see the symmetry with this code I used for testing:
size(550, 550);
translate(10, 10);
//size(530, 530);
for(int j=0, m=23, r=0; j<m*m; r=random(2)>1?m:0)
{
//r=m; //or r=0 for testing
line(j%m*m+r, j/m*m, j%m*m+m-r, j++/m*m+m);
println(j, j%m, j/m);
}
:)
thanks! I left j=-1
just because the angle of the first slash is always determined if you start with j=0
.
Hello,
The choice() function may also be used in some of the examples already provided.
The choice() function is not in the on-line references but can be found in the source code.
If the end goal is to shorten the code for code golf this will certainly do the task!
Each of these performs the same task :
int n = 23;
int r = 0;
r=choice(2);
r=(int)random(2);
r=random(2)>1?1:0;
Example of maze with output to sketch window and also the console:
/*
Project: Maze (10 print chr$(205.5+rnd(1)); : goto 10)
Author: GLV
Date: 2024-06-20
Version: 1.0.0
*/
size(550, 550);
translate(10, 10);
int n = 23;
int r;
stroke(0);
rect(0, 0, n*n, n*n);
for(int j=0; j<n*n; j++)
{
r = choice(2);
// These all return a random 0 or 1.
// Aligned to compare size:
// r=choice(2);
// r=(int)random(2);
// r=random(2)>1?1:0;
int x = j%n;
int y = j/n;
if (j%(23) == 0) println();
if (r == 1) print('/');
if (r == 0) print('\\');
r = r*n;
x = x*n;
y = y*n;
line(x+r, y, x+n-r, y+n);
}
Reference to choice() in Processing source code:
:)
Shaved off a few characters.
This has 95 characters:
size(530,530);for(int j=0,m=23,r=0;j<m*m;r=choice(2)*m)line(j%m*m+r,j/m*m,j%m*m+m-r,j++/m*m+m);
First diagonal line is not random; feel free (anyone) to modify.
That was fun!
:)
wow amazing!! the only improvement I can think of is to get rid of size()
81 char (lines do fill the canvas)
for(int j=0,m=10,r=0;j<100;r=choice(2)*m)line(j%m*m+r,j/m*m,j%m*m+m-r,j++/m*m+m);
75 char (lines don’t fill the canvas)
for(int j=0,r=0;j<81;r=choice(2)*9)line(j%9*9+r,j/9*9,j%9*9+9-r,j++/9*9+9);
It should not be so small that the maze is not a challenge or a poor presentation.
This may be a compromise and still keep it small (for that challenge):
println(g.width, g.height);
println(100/81.0); //1.234567890123... How cool is that!
println(10/8.1); // Curious!
//size(100,100);
scale(1.2345); // Number of decimal places can be reduced
//scale(10/8.1);
for (int j=0, r=0; j<81; r=choice(2)*9)
{
line(j%9*9+r, j/9*9, j%9*9+9-r, j++/9*9+9);
}
I came across this discussion related to 100/81:
https://www.reddit.com/r/askmath/comments/1byjkbe/100_8100_00123456789_repeating/?rdt=44218
Math and coding is just so much fun!
:)
Hello again!
I tried with p5.js and your code example to start with and bit manipulation to replace % and /.
110 characters:
m=16;setup=_=>{for(createCanvas(n=m*m,n),j=0;j<n;)r=random([0,m]),x=j&15,line(x*m+r,y=(j++>>4)*m,x*m+m-r,y+m)}
m=16;
setup=_=>
{
for(createCanvas(n=m*m,n),j=0;j<n;)
r=random([0,m]),
x=j&15,
line(x*m+r,y=(j++>>4)*m,x*m+m-r,y+m)
// Not shorter:
//x=(j&15)*m,
//line(x+r,y=(j++>>4)*m,x+m-r,y+m)
}
I thought I may be able to take advantage of bit manipulation to code golf (shorten) the code.
:)
great! m=16 can go inside random
108
setup=_=>{for(createCanvas(n=256,n),j=0;j<n;)r=random([0,m=16]),x=j&15,line(x*m+r,y=(j++>>4)*m,x*m+m-r,y+m)}
Hello,
p5.js version with bit manipulation and createCanvas() removed.
94 characters
n=256;setup=_=>{for(j=0;j<n;)r=random([0,m=6.25]),x=j&15,line(x*m+r,y=(j++>>4)*m,x*m+m-r,y+m)}
n=256;
setup=_=>{
// console.log(width, height); 100x100
for(j=0;j<n;)
r=random([0,m=6.25]), //100/16 = 6.25
x=j&15,
line(x*m+r,y=(j++>>4)*m,x*m+m-r,y+m)
}
:)
Cool!! I think n is not needed anymore