Newbee Question about random numbers

B

Bob

I'm new to programming. This is a school project that I'm working on.
At one part of this project, I need to scramble a String array to get
the words in array mixed up. I know how to use Math.random() to get
random numbers between 0 and the length of the array, but it will give
me repeated random numbers. How can I get unique random numbers? Also
I have those words displayed as buttons in an applet, any hint how can
I swap 2 words when 2 buttons are clicked? Any help would be greatly
appreciated.
 
O

Oscar kind

Bob said:
I'm new to programming. This is a school project that I'm working on.
At one part of this project, I need to scramble a String array to get
the words in array mixed up. I know how to use Math.random() to get
random numbers between 0 and the length of the array, but it will give
me repeated random numbers. How can I get unique random numbers? Also
I have those words displayed as buttons in an applet, any hint how can
I swap 2 words when 2 buttons are clicked? Any help would be greatly
appreciated.

You cannot create unique random numbers: they wouldn't ne random. But if
you swap the elements at each index (at the time of the swap) of the
array with another random element, the array is randomized anyway.

To swap 2 words in the buttons, use the JButton#setText(String) method.
For other answers to this kind of question, please read the API
documentation: http://java.sun.com/j2se/1.5.0/docs/api/
 
T

Tor Iver Wilhelmsen

I need to scramble a String array to get the words in array mixed
up.

Don't reinvent the wheel: Use
Collection.shuffle(Arrays.asList(theStringArray)).
 
O

Oscar kind

Tor Iver Wilhelmsen said:
Don't reinvent the wheel: Use
Collection.shuffle(Arrays.asList(theStringArray)).

Ooh, I need to read the API docs more often. Thanks Tor!
 
M

marcus

Bob, you are almost there. Tor gave you the shortcut answer, but you
need to learn to think things through.

you jumped from "scramble" to "unique random numbers", but never quite
completed the thought. This is essential in programming, and problem
solving in general.

Shuffling is not picking random numbers out of a hat -- you got that.
But you stopped at what shuffling is. Shuffling is rearranging a list
in random order with a one-to-one correspondence. So the question is,
how to take a list and randomize the *order* of the elements, which is
not, as you pointed out, as simple as generating random numbers.

You would do well to avoid Collection.shuffle() IMHO, and figure out how
to do your homework -- you might learn something along the way.
 
J

Jacob

marcus said:
it is a kind of thing in this ng to not give homework answers

There is no such "kind of thing".

If you don't like to post answers to assignments, then
don't. Others will decide for themselves what to do.

Anyone can ask questions or launch discussions. If the
feedback solves an academic or an enterprise problem
should be the same.

In general: Respond to *good* problems and ignore *bad*
ones. This encourage good questions and discussions and
will keep the quality of the stream high in the long run.
 
O

Oscar kind

marcus said:
it is a kind of thing in this ng to not give homework answers, [...]

I will if the student shows (s)he is willing to learn. Besides, Tor's
answer showed simply that someone had forgotten to read the API. So did I.

Now, if this person would continue to let us find the methods he needs,
I'm sure people will stop answering him. But until that time: if he posts
good questions, he can get an answer from me.
 
P

Paul Lutus

Jacob said:
There is no such "kind of thing".

Yes, there is, at least among professionals: professional teachers, so
students actually learn how to program, and professional programmers, so
they don't have to work with incompetents that were graduated on false
pretenses.

I don't need to ask which of these categories you fit into.
 
J

J.F. Cornwall

Jacob said:
There is no such "kind of thing".

If you don't like to post answers to assignments, then
don't. Others will decide for themselves what to do.

Anyone can ask questions or launch discussions. If the
feedback solves an academic or an enterprise problem
should be the same.

In general: Respond to *good* problems and ignore *bad*
ones. This encourage good questions and discussions and
will keep the quality of the stream high in the long run.

Seems to me that the pointer to information was a good answer. It did
*not* provide the student with code that does his work for him, it gave
him something to look at so he can implement it on his own. Geez....

Jim C
 
M

marcus

Jacob -- if I had looked a little deeper I would have seen tor is a
regular and usually more careful

but . . . giving an API instead of encouraging thought is -- believe it
or not -- bad for everyone.
 
J

J. F. Cornwall

marcus said:
Jacob -- if I had looked a little deeper I would have seen tor is a
regular and usually more careful

but . . . giving an API instead of encouraging thought is -- believe it
or not -- bad for everyone.

I disagree. Knowing just where to look in the API is a tough thing to
figure out sometimes, especially for those of us trying to shift
languages and move from a procedural mindset to the whole object
mindset. If you give a pointer to what part of the API to look at, it
will still require plenty of thought to implement the code. I don't
think that turns out bad for everyone...

Jim C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top