programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Knute Snortum
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Ganesh Patekar
• Frits Walraven
• Tim Moores
• Ron McLeod
• Carey Brown
Bartenders:
• Stephan van Hulst
• salvin francis
• Tim Holloway

Fill an array with random numbers

Greenhorn
Posts: 23
Hello!Nice to meet you all!
i am new to java!I started learning 3 months ago and i have a query on an exercise i have to do.

My exercise is to create an array of 1000 positions and i have to fill it with random numbers. <--Until this point is easy.At least i made it!
To part which is difficult to me is the one were i must create a method which must choose a random Position of the array everytime and based on that position --lets say k--to sort the number in the array with ascending order until k and from position (k +1) to sort with descending order the rest numbers.

Well,i am confused on how to find this random Position..
Any Ideas,please?

****Sorry,if my english isnt the best******

author & internet detective
Posts: 38906
684
John,
Welcome to CodeRanch!

Java comes with a random number generator. Your professor is asking you to instantiate a Random class and call one of it's methods to get a random integer between 0 and the size of your array. (be careful with off by one errors)

John Kapas
Greenhorn
Posts: 23
you mean i must use the same method i used to fill the array with random numbers but this time for the position?
thats where its confusing me.

An example for random numbers is the following:

int Array [] = new int [100] ;
for (int i = 0 ; i < Array.lenght; i++ ) {
Array[i] = (Math.Random() * 200) }

How is Random gonna work for position?? Thats where its confusing me

Jeanne Boyarsky
author & internet detective
Posts: 38906
684
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?

Marshal
Posts: 61697
193
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.

John Kapas
Greenhorn
Posts: 23

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.

i read the links.But the thing is that i want to solve the exercise with things i have learned until now...For instance for sorting i will use stacks and queues..Just to see how they work...
Thanks anyway foe the links.
but i would prefer a little help with the random position (well,the simplest way to do it--for now--)

Sheriff
Posts: 5443
147
• 1

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.

John Kapas
Greenhorn
Posts: 23

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.

really?

John Kapas
Greenhorn
Posts: 23

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?

i have array[10000]
and random numbers between 1 to 100000

and then i have to somehow find a random position K to sort the array in 2 ways

Campbell Ritchie
Marshal
Posts: 61697
193
So what is the smallest permissible value of K and what is its largest permissible value?

John Kapas
Greenhorn
Posts: 23

Campbell Ritchie wrote:So what is the smallest permissible value of K and what is its largest permissible value?

Array[0] and Array [9999] ?

Jeanne Boyarsky
author & internet detective
Posts: 38906
684

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 #.

John Kapas
Greenhorn
Posts: 23

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 #.

The range is known yes but I don't know how to set the rules. For example is something like that:
FOR (A[I] > A[0] and A[I] <= A[9999]; I ++) {
A[k] = math.random () * A[I] ;

And also, let's assume that we know A[k] how can I find A[k+1] position??

It's a part which is confusing to me

Saloon Keeper
Posts: 5137
54
To find a single position there's no loop involved.

John Kapas
Greenhorn
Posts: 23

Carey Brown wrote:To find a single position there's no loop involved.

You mean I should use an If?

Sorry if I am asking to many things but this part of the exercise is terrorize me for a few days now..
If I am,  at least, thinking correctly  for its solution it's a miracle!!

Carey Brown
Saloon Keeper
Posts: 5137
54
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)?

John Kapas
Greenhorn
Posts: 23

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)?

Goodmorning!Happy New Year!!

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..

Sheriff
Posts: 12739
210
Stop thinking about assigning a random value to A[k]. You already did that part. 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
Greenhorn
Posts: 23

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.

Then how am I supposed to do that???

Knute Snortum
Sheriff
Posts: 5443
147

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..

I think you're making it much harder than it is.

An array is made of elements.  The first element in A is A[0].  So if k is the index to A and you want the first element, k should be set to 0.  You would access the first element with A[k].  Does that make sense?

If it does, now what do you have to set k to for a random element?

Carey Brown
Saloon Keeper
Posts: 5137
54

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?

John Kapas
Greenhorn
Posts: 23

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?

I accidentally put 1 more zero. Sorry!!
One array with 1000 positions.

But was there a difference if there were 1000 or 10000 positions?

It's still confusing how to pick the random position

Rancher
Posts: 3311
33

how to pick the random position

Look at the methods in the Random class.  There are some that will do that.

John Kapas
Greenhorn
Posts: 23

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.
At least I can't find one.

Norm Radder
Rancher
Posts: 3311
33

There is nothing like that.

You are looking for a method that returns an int value in a specific range.
Start by looking at all the methods that return an int value.
See if any of those will solve your problem.

If you have questions about any of the methods, copy the text from the API doc for that method and paste it here with your questions.
It is important that you learn how to read the API doc so you can find needed methods.

Carey Brown
Saloon Keeper
Posts: 5137
54
• 2
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)

John Kapas
Greenhorn
Posts: 23

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)

I searched the Random class but I can find something about positions.
So, I guess I won't solve the exercise. Since I can't find anything.

Norm Radder
Rancher
Posts: 3311
33

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
Greenhorn
Posts: 23

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.

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
Saloon Keeper
Posts: 5137
54

John Kapas wrote:So, I guess I won't solve the exercise.

If you can't solve this for an assignment I strongly suggest that you solve it in order to progress in your Java learning. This is a fundamental piece to the understanding of how to use arrays.

Norm Radder
Rancher
Posts: 3311
33
Did you look at the methods in the Random class that return an int?
A random int from the method is what you need to use to select a random position in an array.

I have used only the random method

Look at the Random class.  It has better methods than the Math class for your problem.

I don't know/can't find anything about position

Use an int value to select a "position" in an array.
For example the first position in an array has an index of 0.
The second position in an array has an index of 1.
etc

Carey Brown
Saloon Keeper
Posts: 5137
54

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.

John Kapas
Greenhorn
Posts: 23
• 1

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.

I am not sure if the one I found is the correct one..
Nextint(int n)? That's the one I must use?
I don't know to use it though..

Norm Radder
Rancher
Posts: 3311
33
Do you know how to define a class and save the reference to it in a variable?
Then you can use that variable to call its methods.

Define an instance of the Random class, save a reference to that instance and use it to call the nextInt() method.  Save the value that is returned and print it.
Run the program several times to see what different values are printed.

Carey Brown
Saloon Keeper
Posts: 5137
54
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).

John Kapas
Greenhorn
Posts: 23

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).

I will try use it but I can't promise that my attempt will be successful. Will you help me correct my mistakes?

Norm Radder
Rancher
Posts: 3311
33
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.

John Kapas
Greenhorn
Posts: 23

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.

I will try then to use it in the program and in  case it doesn't work I will ask you to help me. OK??

Norm Radder
Rancher
Posts: 3311
33
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.

John Kapas
Greenhorn
Posts: 23

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.

That's what I am gonna do in few hours. Just creating an array and try to choose random position to see if it works. If not I am doomed

 It is sorta covered in the JavaRanch Style Guide.