- Problem Generating Random Letters
- Posted by Rhonty on December 14th, 2003
Hi
I am trying to write a prgram that will on pressing button1,generate 3
random letters in the range a-e and output them to textField1.
I have been able to do this using the code below however I think this
is the long way to do it, and when I come to sort the letters into
alpha order, it will be complicated.
I think I need to create (i) as the index and have (i) return the 3
random letters rather than (k) & (p) but I don't know how. Any help
greatly appreciated.
void button1_actionPerformed(ActionEvent e)
{
char[]letters=new char [3];
{
char i;
for (i=0; i<3; ++i);
{
i =(char)((int)5*Math.random()+(int)'a');
}
char k;
for (k=0; k<3; ++k);
{
k =(char)((int)5*Math.random()+(int)'a');
}
char p;
for (p=0; p<3; ++p);
{
p =(char)((int)5*Math.random()+(int)'a');
}
textField1.setText(String.valueOf(i)+(k)+(p));
}
}
- Posted by John W. Krahn on December 14th, 2003
Rhonty wrote:
Create a list of five characters in sorted order. Randomly remove two
characters and you are left with a list of three random characters in
sorted order.
John
--
use Perl;
program
fulfillment
- Posted by Roger Willcocks on December 14th, 2003
"Rhonty" <lyonrem@aol.com> wrote in message
news:fa786c3f.0312140806.64442ebf@posting.google.c om...
Work out (on paper) how many different outputs are possible, and
then write a program to randomly choose one of them.
--
Roger
- Posted by Roger Willcocks on December 14th, 2003
"John W. Krahn" <krahnj@acm.org> wrote in message
news:3FDCD059.1843D63F@acm.org...
The sample code generates repeated characters, but it's not clear from the
problem specification whether this should be allowed.
--
Roger
- Posted by John W. Krahn on December 15th, 2003
Roger Willcocks wrote:
It wasn't clear to me what language the code was written in and some of
the code made no sense to me.
Rhonty wrote:
It looks like he is declaring an array named 'letters' but he doesn't
use it anywhere.
For each variable i, k and p he is setting the value to 0 and then a
random value and then 1 and then a random value and then 2 and then a
random value. Why change the value of the variables six different
times?
Does String.valueOf operate just on (i) or on the expression (i)+(k)+(p)
and is the expression adding the values of the variables or
concatenating the characters?
John
--
use Perl;
program
fulfillment
- Posted by gswork on December 15th, 2003
lyonrem@aol.com (Rhonty) wrote in message news:<fa786c3f.0312140806.64442ebf@posting.google. com>...
in no particular language...
you could start off with two arrays
myrange[1..5]=a,b,c,d,e
thisSelection[1..3]
then loop three times and choose one letter each time -
thisSelection[loopindex]=myrange[getarandominteger(1..5)]
(btw, the actual functions and data structures will depend on what
your language offers and what you can create)
You can then perform a sort on the contents of myrange and output them
to textfield1 by virtue of the textfield method.
Not sure if you mind choosing the same letter > once, i.e. ending up
with 'aaa'
If you want the functionality of the above for other elements on the
form (or whatever the object is) then you may want to seperate out the
work of filling the array with letters from the event and put it into
a function of its own, e.g. so you can call it from various other form
events too without having to rewrite the code under each event
handler.
- Posted by Jared Dykstra on December 17th, 2003
gswork@mailcity.com (gswork) wrote in message news:<81f33a98.0312150750.665521fb@posting.google. com>...
It's not necessary to make an array, as letters are represented by
numbers (ASCII) and there aren't any gaps in your data set. If your
random function returns a number between 0 and 1, multiply it by the
number of elements in your set (a..e has 5 elements) and then add the
ascii value of 'a' (decimal 97)
char str[4] = {0};
for (int i = 0; i < 3; i++){
float r = rand(seed);
str[i] = 5 * r + (int)'a';
}
---
Jared Dykstra
www.bork.org/~jared