• Post Reply Bookmark Topic Watch Topic
  • New Topic

Party simulation and a random generator  RSS feed

 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a simulation for a party. There will be 4 classes: host, engineer, artist and scientist. Each one of those will have to be placed on a 50x50 field. I have to create a random generator in order to do this and I'm really stuck on this part for many hours now since I've never done random generators before so I hope that I can get some help. (I'm a beginner, yes.)
I'm currently receiving this error:
"Exception in thread "main" java.lang.NullPointerException at Simulator.populate(Simulator.java:88) at Simulator.partyStatus(Simulator.java:101) at Simulator.(Simulator.java:56) at Simulator.main(Simulator.java:41) BUILD SUCCESSFUL (total time: 3 seconds)"

If there is a different, better way of doing it then I'd be more than happy to see it and if you need any more details regarding this then just ask.

Thanks in advance!
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should be
Also your line numbers are off so it's hard to tell exactly which line caused the error.

Your class has several fields: e.g. width, depth, field, simView, so there's no need to constantly be passing these around. Your methods already have access to these.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:should be
Also your line numbers are off so it's hard to tell exactly which line caused the error.

Your class has several fields: e.g. width, depth, field, simView, so there's no need to constantly be passing these around. Your methods already have access to these.

Thanks for the reply!

Ok so regarding that 'random' line. There's a Random Generator class that I've to use so that's why that's there. If I only leave 'Random' there, it asks me to create a method so not sure what to do here.
Sorry about the number lines. These are the lines that it highlights:
at Simulator.populate:

at Simulator.partyStatus:

at Simulator.<init>:

at Simulator.main:

Hope that helps.
And when it comes to the fields, I guess you mean that the code is not clean? Sorry, I'm still learning. As long as I can get it to work, I'll make it easier to follow after.

Thanks.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is allocating a reference to an ArrayList but not the ArrayList itself, so it is null. You need

Also, here's a code tag trick. I see you are using code tags when posting your code, which is a good thing. When you lop off the beginning of the file the line numbers no longer match the original. You can fix this by modifying the beginning code tag like this
(code=java:firstline(42))
Where the parenthesis are replaced by square brackets and 42 is replaced by the line number you want for the first line of your posted code.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
This is allocating a reference to an ArrayList but not the ArrayList itself, so it is null. You need

Also, here's a code tag trick. I see you are using code tags when posting your code, which is a good thing. When you lop off the beginning of the file the line numbers no longer match the original. You can fix this by modifying the beginning code tag like this
(code=java:firstline(42))
Where the parenthesis are replaced by square brackets and 42 is replaced by the line number you want for the first line of your posted code.


It works! Thank you soo much!
And thanks for the tip regarding the code formatting, I'll make sure to use that next time.

I have one more question now regarding the generation of the dots on the field.
Every time I run it, I end up with the same amount of people on the screen: 250 hosts, 408 scientists, 327 engineers and 512 artists, in the same place. What am I doing wrong here?

Thanks.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are seeding the Random object, e.g. new Random(12345L), you'll get the same answer all the time.

There is also a problem with your loops that allocate people. You use "else if" all the way down which means that the first probability match will cause any following ones to be ignored. Off hand I don't have a solution, I'll think about it. Maybe you can come up with one.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a solution to your "else if" problem. Each case has a 20% probability.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Here's a solution to your "else if" problem. Each case has a 20% probability.


Thank you for this. I haven't used switch statements that much you see, I was used to doing more loops.
Anyway, I think I've done something wrong because now I get a different outcome but it's still the same every time.

Here's the code:



I've commented out that getRandom line too as I wasn't sure whether to include that or not but the outcome doesn't change either way.
I assume it's probably a newbie mistake so I'm sorry about that.

Thanks.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Mathers wrote:I've commented out that getRandom line too as I wasn't sure whether to include that or not but the outcome doesn't change either way.
I assume it's probably a newbie mistake so I'm sorry about that.
Thanks.
Question remains: are you seeding the Random object?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the code for RandomGenerator.getRandom()
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Post the code for RandomGenerator.getRandom()




By the way, this was already given to me and I'm not supposed to change anything here.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Mathers wrote:

By the way, this was already given to me and I'm not supposed to change anything here.


On line 17 you are seeding it with 27. When you seed a random number generator it GUARANTEES that you'll get the same result.

You'd have to replace it with
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Marshall Mathers wrote:

By the way, this was already given to me and I'm not supposed to change anything here.


On line 17 you are seeding it with 27. When you seed a random number generator it GUARANTEES that you'll get the same result.

You'd have to replace it with


I see, that makes sense. It's working perfectly now! I'm not sure why that was there then...
Either way, thank you so much for your help! If I'm stuck on something else, I'll make sure to add a post on here, instead of creating a new topic.

Thanks again.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Mathers wrote:Either way, thank you so much for your help! If I'm stuck on something else, I'll make sure to add a post on here, instead of creating a new topic.

Thanks again.

Glad I could help.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a Simulator object and initialiseWithSeed gets called with the seed parameter, how does the program work? I don't see seed being initialized anywhere, nor is it randomized.
The way I see it, seed from the RandomGenerator class is assigned the value of the parameter; I don't know but maybe that's 0 by default since it was never initialized in the Simulator class.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good point. The call to initialiseWithSeed(seed) should be removed if you want an un-seeded Random object.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Mathers wrote:

By the way, this was already given to me and I'm not supposed to change anything here.

If this is still true, then why are folks suggesting changes and why are you making them?

I suspect that if your instructor said that you shouldn't modify this, he/she is using the deterministic nature of RandomGenerator to test submissions. He/she knows that 27 will always be used as the seed so an automated  test can be written against any implementations that rely on that behavior.

If you want to test for your own purposes, your test code should call the initialiseWithSeed() method so you can see what happens with different pseudo-random number sequences.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Marshall Mathers wrote:

By the way, this was already given to me and I'm not supposed to change anything here.

If this is still true, then why are folks suggesting changes and why are you making them?

I suspect that if your instructor said that you shouldn't modify this, he/she is using the deterministic nature of RandomGenerator to test submissions. He/she knows that 27 will always be used as the seed so an automated  test can be written against any implementations that rely on that behavior.

If you want to test for your own purposes, your test code should call the initialiseWithSeed() method so you can see what happens with different pseudo-random number sequences.

The OP wanted random results. The original suggestion was to replace this line
with
That would have yielded the desired outcome without changing the "code that should not be changed". There seems to be somewhat of a conflict between getting random results vs using the given random generator code.

Using initialiseWithSeed() will not give you random results.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
The OP wanted random results. The original suggestion was to replace this line
with
That would have yielded the desired outcome without changing the "code that should not be changed". There seems to be somewhat of a conflict between getting random results vs using the given random generator code.

Using initialiseWithSeed() will not give you random results.

Well...

And if you think that's ugly, just refactor:
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Well...

And if you think that's ugly, just refactor:

Or
Contortions to get around limitations of the RandomGenerator code. Yuck.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Contortions to get around limitations of the RandomGenerator code. Yuck.

Either that or flout the directive to not change the code for RandomGenerator and risk having OP get deducted points as a result. Pick your poison.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Or

That would have to be:

because the method takes an int argument, not a long.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the suggestions guys. I did end up changing it a little bit from last time mainly due to the fact that I have to use inheritance and I wasn't sure how to implement a 'ModelConstants' class (Which I need now and wasn't aware of it then) with the switch statement so I ended up using the if loop again. It does provide random results now too, I tested it multiple times and data is always different so that's good.

I do have another question though regarding the simulation of a single step/steps where the objects will move on the screen to the next available field near them and I'm not sure how to make it work with inheritance as well as some additional requirements such as some objects not being allowed to be placed next to other objects (Detailed below).

Let me give you a quick overview of what classes I have and what each one more or less is supposed to do without posting lines and lines of code.

Base classes given to me:
Counter: "Provide a counter for a participant in the simulation. This includes an identifying string and a count of how many participants of this type currently exist within the simulation."
Field: "Represent a rectangular grid of field positions. Each position is able to store a single person."
FieldStats: "This class collects and provides some statistical data on the state of a field. It is flexible: it will create and maintain a counter for any class of object that is found within the field."
Location: "Represent a location in a rectangular grid."
SimulatorView: GUI
RandomGenerator: Posted above
Randomizer:

And in addition to them I have a People class which is supposed to be used as a super class for Host, Artist, Engineer and Scientist. As well as the Simulator (below) and Model Constants (also below).

Now to the question. Like I said above, I have to allow the user to be able to simulate the simulation for one step or more and I'm not really sure how to do that given the requirements:
1. Artists chat only to other artists
2. Scientists like other scientists and artists
3. Engineers like all other engineers, scientists and artists.
4. Hosts don't have preferences and everyone else can move next to the hosts.

So let me show you what I have so far:
Simulator


I know there is a lot of commented out code and most likely some duplication but I just wanted to include it too just in case I have to reuse something so just didn't want to delete it.

ModelConstants


Finally, I thought I'd mention that 'Field' has methods such as getObjectAt, place (as in placing an object) and freeAdjacentLocation so I guess they might be of use but not going to post it now as it's already a lot but if you need them or anything else then just ask.
Thanks in advance!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!