0

I recently read that you do not want to make new instances when unnecessary. I am not sure if I understand what it was saying. The problem was the repeat of this in LogCat:

08-09 17:12:11.300: D/dalvikvm(19620): GC_FOR_ALLOC freed 2281K, 23% free 9365K/12048K, paused 12ms, total 12ms

So is it better to have this method:

public int rand(int i) {
    int rand = new Random().nextInt(i);
    return rand;
}

than to have this method:

public int rand(int i) {
    return new Random().nextInt(i);
}

if you are calling rand() a lot? Why or why not? Thanks for helping the newb!

Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • 2
    I don't think you quite understood what you read. Both examples you show are the exact same thing and create a new instance of `Random` – Brian Roach Aug 09 '13 at 21:32

4 Answers4

4

I'd bet those two methods will generate identical bytecode. What you actually want to achieve is to have one instance of Random, and just call nextInts from that instance, as to not reset the seed all the time.

Zavior
  • 6,412
  • 2
  • 29
  • 38
  • ty. i dont quite understand what u mean thought. can u try explaining it differently please? if i only had one instance of random, would it not be the same number repeatedly? – Evorlor Aug 09 '13 at 21:29
  • 1
    http://stackoverflow.com/questions/3816952/java-advantage-of-using-static-variable-in-java This questions has some relevance. Do note, that both of the methods you posted most likely end up 100% identical when compiled. – Zavior Aug 09 '13 at 21:32
  • The `Random` object maintains internal state so that it'll return a different number each time. – yshavit Aug 09 '13 at 21:32
  • im not familiar with internal state, so i guess i should do some more research. thanks for help everyone. not quite sure why i got the downvote though. guess cuz the answer was too obvious to that person – Evorlor Aug 09 '13 at 21:34
  • The internal state(same seed) is important if you care about determinism. – Zavior Aug 09 '13 at 21:37
  • is it possible to only have one instance of Random given that every time i call it, its from a different number of chioces? for example, the first time i call, i may want random between 1 and 10, and then 1 and 5 the next time – Evorlor Aug 09 '13 at 21:39
4

Do this:

class ... {

  private final Random dice = new Random();

  public int rand(int i) {
    return dice.nextInt(i);
  }

}

That way, you are calling new Random() only once, instead of every time you generate a number.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    It will work without it, but instance variables should be `final` unless you *need* to change their value. It's just good practice. – erickson Aug 09 '13 at 21:51
  • It work just fine, but you can't assign a new Random instance there(shouldn't need to do this anyway). – Zavior Aug 09 '13 at 21:52
1

Performance is merely incidental here. If you are creating a new Random object on every call, not only are you wasting time, but you're not getting random numbers at all. You MUST create the Random object only once to create a generator, after which calling various next... functions on it will produce random numbers from that generator. That's how random number generators work.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
-2

It looks to me like you're making things more complicated than they need to be. Instead of calling a method that creates a Random object and returns a new random number, just create a Random object in the method that called your rand(int i) method. For example, instead of doing this:

public void someMethod()
{
    int randomInt = rand(5);
}

Do this:

public void someMethodImproved()
{
    Random randObject = new Random();
    int randInt = randObject.nextInt(5);
}

You can then continue to call randObject.nextInt(5) for as long as you want, and it will continue to give you pseudo-random integers between 0 (inclusive) and 5 (exclusive).

TheDuke777
  • 33
  • 1
  • 7