Adding randomness to your designs

Here is some OpenSCAD code you can add to your design to incorporate into your parameters:

/////////////////////////////////////////////////////////////////// 
// CONTROLLING THE RANDOMNESS 

// controls scaling of elements in random subsequence1 
// number from 0 (min allowable) to 10 (max allowable) style1 = 7; 

// controls likelihood of true/false in random subsequence2 
// number from 0 (always false) to 10 (always true) style2 = 8;

///////////////////////////////////////////////////////////////////
// RANDOM SEED

// LET OPENSCAD CHOOSE A RANDOM SEED
random_seed = floor(rands(1,9999999,1)[0]);

// OR TYPE IN A SEED MANUALLY
// Overrides the slider seed unless set to 0
custom_seed = 0;

// set the random seed
seed = (custom_seed==0) ? random_seed : custom_seed;

// Show random seed? (it won't print even if you say yes)
show_seed = "yes"; //[yes:Yes,no:No]

// Create a string to output the random seed
// thank you laird for showing me how to do this with your code
// from http://www.thingiverse.com/thing:188481
labelString=str(floor(seed/1000000)%10, floor(seed/100000)%10, 
                floor(seed/10000)%10, floor(seed/1000)%10, 
                floor(seed/100)%10, floor(seed/10)%10, 
                floor(seed/1)%10);

// Display the random seed in the render (F5 only)
if(show_seed=="yes"){
    translate([0,-30,0]) 
        color("gray")
        %linear_extrude(height=1)
            text(labelString,size=4,halign="center");
}

// Output the random seed in the log
echo(labelString);

///////////////////////////////////////////////////////////////////
// RANDOM SEQUENCES

// construct the main random sequence
// a list of random real numbers in [0,10] based on the chosen seed
maxSteps=30*1;
random = rands(0,10,maxSteps,seed);
echo(random);

// subsequence of 10 items from the main sequence
// weighted by style1
subrandom1 = [for(i=[0:10-1]) random[i]*style1];
echo(subrandom1);

// subsequence of another 10 items from the main sequence
// output as true/false verdicts by comparison to style2
subrandom2 = [for(i=[10:10+10-1]) random[i]<=style2]; 
echo(subrandom2);

///////////////////////////////////////////////////////////////////
// EXAMPLE RENDERS

// example: flat base based on overall size parameters
cube([100,30,10],center=true);

// example: cubes 
// heights based on subrandom1, visibility based on subrandom2
color("white")
for (i=[0:10-1]){
    if (subrandom2[i]==true){
        translate([(i-5)*(100/12)+100/24,0,5+subrandom1[i]/2]) 
        cube([5,5,subrandom1[i]],center=true);
    }
}

Here is the screenshot we discussed in class; this is part of some more complicated randomized code but the way the random numbers are being used is simpler. Notice that each time we need another random number we just choose the next one in the list “random” and use it where it is needed.

Easier code for random parameters

From your first round of random projects I can see that I could have provided some simpler random-parameter code. My earlier code used subsequences which I think hindered what you were able to design last week. Here is some much simpler code. Start again with this!

///////////////////////////////////////////////////////////////////
// CHOOSE ONE RANDOM SEED AND THEN A RANDOM LIST

// LET OPENSCAD CHOOSE A RANDOM SEED
random_seed = floor(rands(1,9999999,1)[0]);

// OR TYPE IN A SEED MANUALLY
// Overrides the slider seed unless set to 0
custom_seed = 0;

// set the random seed
seed = (custom_seed==0) ? random_seed : custom_seed;

// a list of 30 random real numbers in [0,10] based on the chosen seed
// to use these in the code type random[0], random[1], random[2], and so on
maxSteps=30;
random = rands(0,10,maxSteps,seed);

///////////////////////////////////////////////////////////////////
// DISPLAY THE SEED 

// Show random seed? (it won't print even if you say yes)
show_seed = "yes"; //[yes:Yes,no:No]

// Create a string to output the random seed
// thank you laird for showing me how to do this with your code
// from http://www.thingiverse.com/thing:188481
labelString=str(floor(seed/1000000)%10, floor(seed/100000)%10, 
                floor(seed/10000)%10, floor(seed/1000)%10, 
                floor(seed/100)%10, floor(seed/10)%10, 
                floor(seed/1)%10);

// Display the random seed in the render (F5 only)
if(show_seed=="yes"){
    translate([0,-30,0]) 
        color("gray")
        %linear_extrude(height=1)
            text(labelString,size=4,halign="center");
}

// Also output the random seed and the resulting list of random numbers in the log
echo(labelString);
echo(random);

///////////////////////////////////////////////////////////////////
// OK NOW MAKE SOMETHING!

cylinder(h=2*random[0]+10, r=2*random[1]);