This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Fill an array with random numbers  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 38504
653
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 38504
653
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 5028
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the smallest permissible value of K and what is its largest permissible value?
 
John Kapas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 38504
653
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To find a single position there's no loop involved.
 
John Kapas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 5028
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4522
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!