We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › creating a random word as argument for an object
Page Index Toggle Pages: 1
creating a random word as argument for an object (Read 889 times)
creating a random word as argument for an object
Jul 10th, 2009, 1:45pm
 
Hello i need to create a random word as argument for an object creation. The word can be any random word created by the conbination of these letters: a b c d e.
A word can be for example:
"bb",  "ace"  ,"a" , "dabc" , "bbab" ,"eeabc", etc.


Which is the best way of doing these in processing?

thanks

Re: creating a random word as argument for an object
Reply #1 - Jul 10th, 2009, 3:20pm
 
Who knows if it is the best way. but it is definetly one way :


String[] myLetters = { "a", "b","c","d","e" };
String word;
int wordLength = 5;

void Setup(){
size(200,200);
}
void draw(){
}

void mousePressed(){
word = "";
for(int i = 0 ; i<1+int(random(wordLength)); i++){
word=word+myLetters[int(random(wordLength))];
}
println(word);
}
Re: creating a random word as argument for an object
Reply #2 - Jul 10th, 2009, 3:20pm
 
Put the letters in an array (the characters can be generated, but this way is more generic if you pick up other letters, for example).
Draw a random number: the length of the word.
Iterate the number of times, draw a random number between 0 and the length of the array (excluded): it will be the next letter to add to the word.
Re: creating a random word as argument for an object
Reply #3 - Jul 10th, 2009, 3:23pm
 
Smiley
Re: creating a random word as argument for an object
Reply #4 - Jul 10th, 2009, 8:51pm
 
Follow PhiLho's suggestion. You may find problems with mixing types (what is in the array Characters or one-letter strings), but the general method is sound.

There are problems with Cedric's suggestion (aside from possibly "doing someone's homework"). Problem areas identified below, leaving it up to the reader to consider the problems and/or improvements that could be made.

Cedric wrote on Jul 10th, 2009, 3:20pm:
String[] myLetters = { "a", "b","c","d","e" };
String word;
int wordLength = 5;

void Setup(){
size(200,200);
}
void draw(){
}

void mousePressed(){
word = "";
for(int i = 0 ; i<1+int(random(wordLength)); i++){
word=word+myLetters[int(random(wordLength))];
}
println(word);
}


There are other issues, such as whether the word-generation functionality should be factored out into a separate method, but the points highlighted are problems with the method in general.

-spxl
Re: creating a random word as argument for an object
Reply #5 - Jul 10th, 2009, 11:44pm
 
Like i mentioned, its probably not the best way. So maybe i will learn something too...
Only thing i can see is, that i should have called it rather "maxWordLength" instead of "wordLength"...

but give us some hints please.
Re: creating a random word as argument for an object
Reply #6 - Jul 11th, 2009, 12:30am
 
subpixel is right: you use wordLength as max index in the array, where it sould be myLetters.length. Your code works because wordLength is smaller, but would crash randomly otherwise.
Moreover, unlike some other languages, Java re-evaluates the test part on each loop, so you have a moving target there. It will end thanks to test < (might never stop if is was !=) but it is not predictable. It is better to get the bound before starting the loop.
Demonstration:
Code:
void setup()
{
 for (int i = 0; i < Foo(); i++) {}
}
int Foo()
{
 int x = int(random(100));
 println(x);
 return x;
}

Improvement:
for(int i = 0, b = 1+int(random(maxWordLength)); i < b; i++)
Re: creating a random word as argument for an object
Reply #7 - Jul 11th, 2009, 12:47am
 
Oh now i got it. Sure, i totally understand. Obvious mistake I made.  Thanks for pointing it out.
Re: creating a random word as argument for an object
Reply #8 - Jul 11th, 2009, 1:26am
 
Yes, that's the problem with quickly thrown demo code, you probably wouldn't make such mistakes on your real programs. Describing algorithms is less prone to implementation errors... Wink

What is nice here is that pointing out the mistakes might also help the learning experience of beginners. Smiley
Re: creating a random word as argument for an object
Reply #9 - Jul 11th, 2009, 2:09am
 
probably not.  Smiley These are the kind of mistakes that are easily to spot as soon as they cause problems but as long as it works it was not obvious to me.
Page Index Toggle Pages: 1