programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Why is my random method not very random?

tom davies
Ranch Hand
Posts: 168
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
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
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
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
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
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
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
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
• 1
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