• 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

Merge Sort with Random Numbers

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys I have a question this might be a stupid one but here goes nothing! How do i make this merge sort take on random numbers in an array instead of hard coding the numbers


 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris, welcome to the Ranch.


Couple of things though that might help make your stay more enjoyable
- You were originally SHOUTING in your subject line and post. Plesae KeepItDown.
- When you post code, please UseCodeTags. There is a handy button for it

I've fixed it all up this time, just keep it in mind for any further questions.

To get to your question, it looks like you have a nice modular bit of code, all you have to do is create an array of random numbers.
There are a couple of options:
Math.random() will give you a Random number.
You can also use the java.util.Random class directly.

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris, welcome to CodeRanch!

You know how to do stuff multiple times right? Use a for-loop! For each iteration, generate a random number (take a look at java.lang.Math) and store it in your array.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want the array filled with random numbers (in which case you can have repetition), or a random permutation of 1-N? If the former, you want Stephan’s approach. If the latter you've got a couple of options, but I'd put the numbers in a List and use Collections.shuffle().
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a quicker way to fill an array with pseudorandom numbers:
Create a Random instance.
Use its method which creates a Stream, e.g. this.
The Stream has a method which turns it to an array automatically.
Somebody else had a similar problem yesterday. This method doesn't work in Java7.
 
If you are using a wood chipper, you are doing it wrong. Even on this 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