• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fill an array with random numbers

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just creating an array and try to choose random position  


Don't make the simple test program too complicated.  Just create a random number in a specific range and print it.  Nothing more to keep the test simple.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

Just creating an array and try to choose random position  


Don't make the simple test program too complicated.  Just create a random number in a specific range and print it.  Nothing more to keep the test simple.



But the point here is to create a random position of the array not to fill it with random numbers.

Anyway I am trying it now
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I am suggesting:
First learn what a tool does and how to use it.
Then use the tool to solve your problem.

the point here is to create a random position of the array not to fill it with random numbers.  


That statement contradicts the title of this thread: Fill an array with random numbers
Can you state your objective for this program?

What would be the purpose of randomly selecting a position in an empty array?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly, OP said th idea was to sort the array below the random position and to sort the remainder of the array backwards. That does seem a strange requirement, so I might be mistaken there.
 
John Kapas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What I am suggesting:
First learn what a tool does and how to use it.
Then use the tool to solve your problem.

the point here is to create a random position of the array not to fill it with random numbers.  


That statement contradicts the title of this thread: Fill an array with random numbers
Can you state your objective for this program?

What would be the purpose of randomly selecting a position in an empty array?



Firstly, I don't know who changed the title of this thread since my title was "new to Java. I have queries"

The objective of this program is:
1 create an array
2.fill it with random numbers
3.choose a random position K of the array
4.sort the array with 2 ways.

I am trying to do point 3 which is to choose the random position K of the array. That's where I got confused and I do not know how to do it
 
John Kapas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If I remember correctly, OP said th idea was to sort the array below the random position and to sort the remainder of the array backwards. That does seem a strange requirement, so I might be mistaken there.




Correctly!!!
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I missed what step you were working on.
The Random class's method would work for step 3.choose a random position K of the array
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a code with comments to help you out:


output:

PS: I would love to see you write OPs code and not everything in Main the way I did, I just wanted to write a short code to show you the concepts.
 
John Kapas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Here's a code with comments to help you out:


output:

PS: I would love to see you write OPs code and not everything in Main the way I did, I just wanted to write a short code to show you the concepts.



Thanks for this example but I think it's not exactly what the exercise wants or at least I don't understand it that way.
I think it's something like the "slight variation of method 2".i am writing the code and I will post it tomorrow. I will post everything I have done until now.. And the part about the random array position.

I am trying some things here now to see if they work
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Kapas wrote:. . . Thanks for this example but I think it's not exactly what the exercise wants or at least I don't understand it that way.  . . .

No, the idea behind Salvin's post was to give you a hint of what sort of thing would work, not a direct answer. Giving a direct answer would have been regarded as unfair help by your teachers and might have got you into trouble.
 
John Kapas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

John Kapas wrote:. . . Thanks for this example but I think it's not exactly what the exercise wants or at least I don't understand it that way.  . . .

No, the idea behind Salvin's post was to give you a hint of what sort of thing would work, not a direct answer. Giving a direct answer would have been regarded as unfair help by your teachers and might have got you into trouble.



I will post my code or part of it - - the part at least that is difficult for me - in 2 days. I had a blackout and my pc isn't working. It will take 2 days to buy one. Right now I am writing here from my phone.
I am reading and the others threads and I thing this is the best forum ever.!!!At least for me because I am one of the Java Dummies..
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic