10 Replies - 1174 Views - Last Post: 28 July 2013 - 05:03 AM Rate Topic: -----

#1 ZeroSumPhase   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 13

Randomly select an int from an Array

Posted 26 July 2013 - 08:14 PM

This is probably simple, but my lack of experience with util.Random is showing >.<

I am trying to create a method named randomItem, which will, upon the push of a GUI button oh-so-aptly named Random, select a random value from an array (if it's of any use, the int we're selecting is called productId).

I've already created an ActionHandler for the button itself (you know, the really easy part)

This is NOT homework; I recently finished my Java class, and I'm messing around with my finished final product, trying to add some new features and learn a few things along the way.

I've played around with a few ideas, like
Random x = new Random().nextInt(items.length);


But I'm having trouble getting it all fleshed out. Anyone want to give me a nudge in the right direction?

Much obliged!

Is This A Good Question/Topic? 0
  • +

Replies To: Randomly select an int from an Array

#2 ConciselyVerbose   User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Randomly select an int from an Array

Posted 26 July 2013 - 08:22 PM

What you have looks OK, except that that would be an int, not a Random.

If you use random elsewhere or multiple times, I would create a Random for the class instead of before you use it so it doesn't need to be re-initialized every time.

Assuming one access,

Item x = items[new Random().nextInt(items.length)];



Your code is correct to get the index. You just need to access it.

Also, I know Item isn't the actual class.
Was This Post Helpful? 1
  • +
  • -

#3 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Randomly select an int from an Array

Posted 26 July 2013 - 09:02 PM

I would avoid creating a new Random object every time you access the array, better keep the Random object as an instance or class variable and pick off random numbers as you need them.
Was This Post Helpful? 1
  • +
  • -

#4 ConciselyVerbose   User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Randomly select an int from an Array

Posted 26 July 2013 - 09:07 PM

Indeed. That gets fairly inefficient quickly.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Randomly select an int from an Array

Posted 27 July 2013 - 01:24 AM

... And less random if you aren't using your own seed which isn't based on current time ;)
Imagine if it was a server creating a new Random each time you do a request. You can then calculate a small range of possible values the Random might have generated based on the current time and response time

This post has been edited by CasiOo: 27 July 2013 - 01:26 AM

Was This Post Helpful? 1
  • +
  • -

#6 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Randomly select an int from an Array

Posted 27 July 2013 - 01:46 AM

On top of that - look at how complicated creating one random object is.
Spoiler


Construction occurs between lines 89 to 132, if you didn't use your own seed.

Now imagine creating that 1000 times in a for loop just to obtain some pseudo-random integers. Your processor will hate you.
Was This Post Helpful? 0
  • +
  • -

#7 ZeroSumPhase   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 13

Re: Randomly select an int from an Array

Posted 27 July 2013 - 12:27 PM

Alright, about 70% of this conversation just went over my head.

I have an array- an inventory of items. The GUI will currently display the item number, name, and additional (read as: useless) information.

What I am trying to do is create a method that will, upon clicking a button, randomly choose one of the item numbers in the array, and display all of its corresponding information. Each time I press the button, it should generate a new result; I don't mind terribly if it occasionally produces the same result (meaning, I don't need it to eliminate previous selections). Additionally, I don't need it to save or display any of its previous selections (because I don't care about them).

Assume for a moment a person is indecisive, and creates an array filled with shirts. Each time they press random, they want a different shirt to be called to the GUI's information fields.


Does this information at all help to clarify what I'm trying (and still failing, I might add) to do?
Was This Post Helpful? 0
  • +
  • -

#8 Draps   User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 103
  • Joined: 20-November 10

Re: Randomly select an int from an Array

Posted 27 July 2013 - 01:07 PM

Post #2 and #3 answer this for you.

You want to use Random() to get your index (the number that goes between the [ ] square braces)

Post #3 suggest making the Random object a class variable so you don't have to keep recreating it each call. (the new keyword)
Was This Post Helpful? 0
  • +
  • -

#9 ConciselyVerbose   User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Randomly select an int from an Array

Posted 27 July 2013 - 01:21 PM

Where you define your class variables, put:

Random random = new Random();


when you want the random item use:

Item x = items[random.nextInt(items.length)];


replacing Item with whatever type your class is.
Was This Post Helpful? 2
  • +
  • -

#10 ZeroSumPhase   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 13

Re: Randomly select an int from an Array

Posted 27 July 2013 - 04:08 PM

View PostConciselyVerbose, on 27 July 2013 - 01:21 PM, said:

Where you define your class variables, put:

Random random = new Random();


when you want the random item use:

Item x = items[random.nextInt(items.length)];


replacing Item with whatever type your class is.


Sigh... why must I over-think everything?

Thanks for breaking this down.
Was This Post Helpful? 0
  • +
  • -

#11 Leopold   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-July 13

Re: Randomly select an int from an Array

Posted 28 July 2013 - 05:03 AM

Just do:
Item item = items[Math.random() * items.length];


Edit:
Item item = items[(int) (Math.random() * items.length)];

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1