discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Rands as module or fucntion

P
pulsar0952@hotmail.fr
Tue, Jan 3, 2023 10:01 AM

Hi there,

I reached to generate 6 characters among 26, but I can’t make it  become a function or module…

here is my code

Vect_rand = rands (1,26,6);

echo (vect_rand);

List = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

Vect_letters = 0;

for(i=[0:5]) {

//echo (i);

Nb_rand = (Vect_rand[i]);

echo (List[Nb_rand]);

//vect_letters[i]=liste[Nb_rand] ;

}

Works fine  :-)

and

essai = aleatoire (param);

echo (essai);

module aleatoire (p1) {

vect_rand = rands (1,26,6);

Liste = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

for(i=[0:5]) {

//echo (i);

nb_alea = (vect_rand[i]);

echo (liste[nb_alea]);

assign (param[i],Liste[nb_alea]);

//param[i]=Liste[nb_alea];

}

}

doesn’t work  :-(

Anyone to help me please ?

Hi there, I reached to generate 6 characters among 26, but I can’t make it become a function or module… here is my code `Vect_rand = rands (1,26,6);` `echo (vect_rand);` `List = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];` `Vect_letters = 0;` `for(i=[0:5]) { ` ` //echo (i);` ` Nb_rand = (Vect_rand[i]); ` ` echo (List[Nb_rand]);` ` //vect_letters[i]=liste[Nb_rand] ;` `}` Works fine :-) and `essai = aleatoire (param);` `echo (essai);` `module aleatoire (p1) {` ` vect_rand = rands (1,26,6);` ` Liste = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];` ` for(i=[0:5]) { ` ` //echo (i);` ` nb_alea = (vect_rand[i]);` ` echo (liste[nb_alea]);` ` assign (param[i],Liste[nb_alea]);` ` //param[i]=Liste[nb_alea];` ` }` `}` doesn’t work :-( Anyone to help me please ?
NH
nop head
Tue, Jan 3, 2023 10:44 AM

Try this:

vect_rand = rands (1,26,6);
echo (vect_rand);
liste =
["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
param = [ for(i=[0:5]) let( nb_alea = vect_rand[i])  liste[nb_alea] ];

echo(param);

param is built with a list comprehension. It could also be used in a
module, or a return value of a function.

On Tue, 3 Jan 2023 at 10:02, pulsar0952@hotmail.fr wrote:

Hi there,

I reached to generate 6 characters among 26, but I can’t make it become a
function or module…

here is my code

Vect_rand = rands (1,26,6);

echo (vect_rand);

List =
["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

Vect_letters = 0;

for(i=[0:5]) {

//echo (i);

Nb_rand = (Vect_rand[i]);

echo (List[Nb_rand]);

//vect_letters[i]=liste[Nb_rand] ;

}

Works fine :-)

and

essai = aleatoire (param);

echo (essai);

module aleatoire (p1) {

vect_rand = rands (1,26,6);

Liste =
["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

for(i=[0:5]) {

//echo (i);

nb_alea = (vect_rand[i]);

echo (liste[nb_alea]);

assign (param[i],Liste[nb_alea]);

//param[i]=Liste[nb_alea];

}

}

doesn’t work :-(

Anyone to help me please ?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Try this: vect_rand = rands (1,26,6); echo (vect_rand); liste = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; param = [ for(i=[0:5]) let( nb_alea = vect_rand[i]) liste[nb_alea] ]; echo(param); param is built with a list comprehension. It could also be used in a module, or a return value of a function. On Tue, 3 Jan 2023 at 10:02, <pulsar0952@hotmail.fr> wrote: > Hi there, > > I reached to generate 6 characters among 26, but I can’t make it become a > function or module… > > here is my code > > Vect_rand = rands (1,26,6); > > echo (vect_rand); > > List = > ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; > > Vect_letters = 0; > > for(i=[0:5]) { > > //echo (i); > > Nb_rand = (Vect_rand[i]); > > echo (List[Nb_rand]); > > //vect_letters[i]=liste[Nb_rand] ; > > } > > Works fine :-) > > and > > essai = aleatoire (param); > > echo (essai); > > module aleatoire (p1) { > > vect_rand = rands (1,26,6); > > Liste = > ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; > > for(i=[0:5]) { > > //echo (i); > > nb_alea = (vect_rand[i]); > > echo (liste[nb_alea]); > > assign (param[i],Liste[nb_alea]); > > //param[i]=Liste[nb_alea]; > > } > > } > > doesn’t work :-( > > Anyone to help me please ? > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
P
pulsar0952@hotmail.fr
Tue, Jan 3, 2023 1:36 PM

nop head ,

Thank you very much. That’s great.

have a nice day! and happy new year  ;-)

nop head , Thank you very much. That’s great. have a nice day! and happy new year ;-)
JB
Jordan Brown
Tue, Jan 3, 2023 4:55 PM

Note that the fundamental problems you were having are:

  • OpenSCAD variables are immutable.  You can only create entire new
    arrays, not modify a single entry in an array.
  • OpenSCAD variables are limited to the scope they are created in.  If
    you create a variable inside the body of an "if" or "for", it is
    visible only in that block - and for a "for", individually for each
    iteration.

When setting variables, the ?: ternary operator and list comprehensions
are your friends.

Note that the fundamental problems you were having are: * OpenSCAD variables are immutable.  You can only create entire new arrays, not modify a single entry in an array. * OpenSCAD variables are limited to the scope they are created in.  If you create a variable inside the body of an "if" or "for", it is visible only in that block - and for a "for", individually for each iteration. When setting variables, the ?: ternary operator and list comprehensions are your friends.
HL
Hans L
Tue, Jan 3, 2023 6:59 PM

Note that you will never generate the first character "A" with that code.

Also the array lookup is not necessary and can be replaced with usage of
ord and chr functions.

Here is a one-liner to do the same thing:
echo([ for(rnd=rands(0,26,6)) chr(ord("A")+rnd) ]);

On Tue, Jan 3, 2023 at 10:57 AM Jordan Brown openscad@jordan.maileater.net
wrote:

Note that the fundamental problems you were having are:

- OpenSCAD variables are immutable.  You can only create entire new
arrays, not modify a single entry in an array.
- OpenSCAD variables are limited to the scope they are created in.  If
you create a variable inside the body of an "if" or "for", it is visible
only in that block - and for a "for", individually for each iteration.

When setting variables, the ?: ternary operator and list comprehensions
are your friends.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Note that you will never generate the first character "A" with that code. Also the array lookup is not necessary and can be replaced with usage of `ord` and `chr` functions. Here is a one-liner to do the same thing: echo([ for(rnd=rands(0,26,6)) chr(ord("A")+rnd) ]); On Tue, Jan 3, 2023 at 10:57 AM Jordan Brown <openscad@jordan.maileater.net> wrote: > Note that the fundamental problems you were having are: > > - OpenSCAD variables are immutable. You can only create entire new > arrays, not modify a single entry in an array. > - OpenSCAD variables are limited to the scope they are created in. If > you create a variable inside the body of an "if" or "for", it is visible > only in that block - and for a "for", individually for each iteration. > > When setting variables, the ?: ternary operator and list comprehensions > are your friends. > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
P
pulsar0952@hotmail.fr
Wed, Jan 4, 2023 10:44 AM

thank you all for your advices

it works now !

thank you all for your advices it works now !