[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Campbell Ritchie wrote:Welcome to the Ranch (again
I gave up trying to do arithmetic with Math#random a long time ago. I hope you will find an explanation here, and the other links given there. I won't tell you that there is a neater way of creating your array with “random” numbers in it, nor that you can find an example of it in that link. Nor that it will raise eyebrows no end if you use it without being taught it. Ask before you use that technique.
You can use an object similar to that shown in those other posts to choose a “random” number within the size of the array. If you go to the API documentation and find the name of the class you want for random numbers, and look at its methods, you should be able to find a method that provides a number in exactly the right range. I shan't say any more: that way you can say you found it yourself with a few hints from somebody else.
but i would prefer a little help with the random position (well,the simplest way to do it--for now--)
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
but i would prefer a little help with the random position (well,the simplest way to do it--for now--)
Jeanne Boyarsky gave you a hint.
Jeanne Boyarsky wrote:John,
Yes, the same method. Pretend for a minute that you were playing a game where you needed to pick random numbers. You'd create pieces of paper from zero to X and put them in a bag. Then you'd pick one at it would be the random index.
So what is X? What is the largest index value you can have?
Campbell Ritchie wrote:So what is the smallest permissible value of K and what is its largest permissible value?
John Kapas wrote:
Campbell Ritchie wrote:So what is the smallest permissible value of K and what is its largest permissible value?
Array[0] and Array [9999] ?
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:
John Kapas wrote:
Campbell Ritchie wrote:So what is the smallest permissible value of K and what is its largest permissible value?
Array[0] and Array [9999] ?
Correct. Now you have the range for your random #.
Carey Brown wrote:To find a single position there's no loop involved.
Carey Brown wrote:You already have
(int)(Math.Random() * 200)
which gives you a random number from 0 to 199 (inclusive). So, how do you compute one between 0 and 999 (inclusive)?
Junilu Lacar wrote: What you're trying to get is a random value for k, which will be a number from 0 to 9999 since your array A has 10000 elements.
John Kapas wrote:
So, you mean for the random position is
A[k] = math.Random () * 9999?
As 9999 are all the positions???
And if my thinking is right the next position
A[k+1] = A[k] +1??
I am asking because I need it for the exercise..
All things are lawful, but not all things are profitable.
John Kapas wrote:i have array[10000]...
Carey Brown wrote:
John Kapas wrote:i have array[10000]...
Your original post said 1000 positions and somewhere along the line we now have 10000. Which is it?
how to pick the random position
Norm Radder wrote:
how to pick the random position
Look at the methods in the Random class. There are some that will do that.
There is nothing like that.
Carey Brown wrote:A[k] is not a position.
k is the position.
k = 42 is a way to hard code the position.
k = /*random int expression*/ is a way to assign a random int to a position. (insert your expression)
something about positions
Norm Radder wrote:
something about positions
What is a position? Methods do not return positions. The Random class methods return numeric values, like an int.
An int value selects a "position" in an array.
In Carey Brown's example the variable k holds an int value that selects a "position" in the array.
John Kapas wrote:So, I guess I won't solve the exercise.
I have used only the random method
I don't know/can't find anything about position
John Kapas wrote:I have used only the random method which is about the random numbers. I don't know/can't find anything about position and how to use it.
Carey Brown wrote:
John Kapas wrote:I have used only the random method which is about the random numbers. I don't know/can't find anything about position and how to use it.
As Norm pointed out, you won't find anything about "position", but a "position" is an int, and if you can't find anything about returning an int in the Javadocs for the Random class then you need to understand how to read Javadocs.
Carey Brown wrote:That's the one. First you have to create an instance of a Random object. Then use that object to call nextInt(n), where 0 is the beginning of the range and 'n' is the end of the range EXCLUSIVE. In other words, to get a range of 0-9 inclusive, you need to call nextInt(10).
Norm Radder wrote:The nice thing about most of the programs we write is that no harm is done if there is a mistake. A program can be rewritten many times when trying to learn new classes and methods.
Norm Radder wrote:I suggest writing a small test program that just uses the Random class's method and prints out what is returned so you can see how to use the method and what it does.
Don't try using it in a larger program until you are familiar with it.