• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code check  RSS feed

 
Zeeshan AliNoxiee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


CAN anyone tell me why the Random rand is initiated at the Constructor and not at roll? Like the code works for both but I remember our teacher telling us something about one uses more memory than the other.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a new Random object (rand) seeds the random number generator, from there on it can calculate new random values without needing to re-seed the generator. In your code you are re-seeding the generator for every Die, which is not necessary. You could do this and only initialize it once ...
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall try adding code tags to your post; they usually make the code look a lot better. Always use them
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't initialise any fields in the roll() method. That would require horrible code like this:-At least you are not going to get the double‑checked locking problem with that code. I shall let you search for information about double‑checked locking.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Can anyone tell me why the Random rand is initiated at the Constructor and not at roll?
>Like the code works for both but I remember our teacher telling us something about one uses more memory than the other.

You remember correctly.
Every time you create a new Random() it would allocate some memory for it. When you stop using it, the memory would be available for garbage collection

Assume you create 5 dice and then call roll() 100 times on each.

Scenario #1: Create the new Random in your roll() method.
Would allocate memory every time you ask for a new random number. And then immediately throw it away at the end of the method. So you would create and then throw away 500 objects.

Scenario #2: Create the new Random with the Die Object
Allocates 5 Random objects (one for each die). They would be released when the Die object is released.

Scenario #3: Static variable for Random in the Die Object (Carey's answer)
Allocates 1 Random object for all die. Would never be released.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote: . . .
Scenario #1: Create the new Random in your roll() method.
Would allocate memory every time you ask for a new random number. And then immediately throw it away at the end of the method. So you would create and then throw away 500 objects.
. . .
In older versions of Java™ there was a risk of all those Random objects returning the same sequence of numbers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!