• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is my random method not very random?  RSS feed

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to simulate a number of checkout lines.
Each checkout line is a LinkedList which is filled with customers.
The method i am trying to create adds customers to a checkout line (LinkedList) depending on the length of the line and also the amount of shopping the customers on each line have.
In the real world you wouldn't just go for the checkout with the least people, you would see how much shopping they had as well which is what i am trying to replicate.
However when i run this, if i add a few hundred customers they all split evenly among the checkouts i have.
If i add 1000 then there are slight variations between each checkout, only by 3-4 people though.
Does anyone have any ideas why they are splitting so evenly.

checkout1 is the first of my checkouts (LinkedList)
and linkedlists is a LinkedList of my checkouts to allow me to loop through them all.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that, by using nextInt, you are getting numbers following a uniform distribution.
This means that they all have the same chance of getting picked.
For random discrete numbers up to 100, the average will be 50.
That's why you're getting the same distribution among each line.

Try using a normal distribution instead. Look at the nextGaussian method.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

A couple of points......

First while Christophe is correct a normal (actually i would a triangular distribution for shopping cart contents rather than an normal distribution, but..) distribution is more closer to what would be found, it would be even closer (what you think of less random) than a uniform distribution.

Now tom, rather than simply answering you question, please do some research on chi-squared analysis and the write a very simple loop calculating the chi-squared analysis, and then go back to how Random should be used. after you correct that and rerun the chi-squared. You will then know how random the random number generator (and your code) is.

-steve
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both, I will read more into the topics you suggested. The nextGaussian seems the simplest method but reading into triangular destruction, that looks like what I need to be most realistic. I will look into how I could possibly write my own function for that.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok am I reading your code completely wrong? Looks like you'll will never add any customers to first queue, initialShopping will always be 0 for the first iteration. CustomerShopping will never be less than initialShopping
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Try using a normal distribution instead. Look at the nextGaussian method.


Umm, a normal distribution? Unless you chose a very large mean value, you could easily get negative values. Not what you want for a shopping cart. Maybe a log-normal distribution?

Sorry, Tom, if this is leading the thread into deep water where you don't need to go. Just tell us to knock it off if that's the case.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Umm, a normal distribution? Unless you chose a very large mean value, you could easily get negative values. Not what you want for a shopping cart. Maybe a log-normal distribution?

You can tweak the mean and the standard deviation, and check for negative values, like done here.
But I agree that some other type of distribution should be chosen for a shopping cart.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Ok am I reading your code completely wrong? Looks like you'll will never add any customers to first queue, initialShopping will always be 0 for the first iteration. CustomerShopping will never be less than initialShopping

Initially all checkouts are set to 0 so customer shopping will not be greater than the initial shopping. The current code does add values to all checkouts uniformly although maybe I should say customersshopping<=initialshopping

As part of this simulation I have to write a report to discuss the model and any enhancements I could make. Learning about how the random numbers are generated and the distributions different methods create will help me think of improvements if nothing else. So it is not a problem Paul
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:In the real world you wouldn't just go for the checkout with the least people, you would see how much shopping they had as well which is what i am trying to replicate.

To be honest, I reckon all this fancy speculation is a bit OTT.

Why not simply sum all the shopping items for each checkout line and choose the one which has the least? You could then introduce some sort of "error factor" (ie, the likelihood of someone making an error in their "item count"), viz something like:There are probably several other ways of doing it though.

Winston

PS: I broke up that very long line of yours because it makes your thread hard to read. Please re-read the UseCodeTags page thoroughly. Thanks.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't checked through this code properly - this is gut feeling - so apologies if it isn't relevant...

...But if you're randomly assigning the amounts of shopping to different shoppers, is it really a surprise that the queues will tend to the same length? The variation of the amount of shopping for N shoppers is less than the variation for one shopper, so I'd expect things to even out.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!