aspose file tools
The moose likes Beginning Java and the fly likes Why is my random method not very random? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why is my random method not very random?" Watch "Why is my random method not very random?" New topic
Author

Why is my random method not very random?

tom davies
Ranch Hand

Joined: Apr 27, 2012
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

Joined: Nov 24, 2005
Posts: 14669
    
  11

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.


[My Blog]
All roads lead to JavaRanch
Steve Fahlbusch
Ranch Hand

Joined: Sep 18, 2000
Posts: 491
    
    2

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

Joined: Apr 27, 2012
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
Bartender

Joined: Jan 17, 2008
Posts: 1262
    
    7

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
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

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

Joined: Nov 24, 2005
Posts: 14669
    
  11

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

Joined: Apr 27, 2012
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

Joined: Mar 17, 2011
Posts: 4725
    
    7

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.

Isn't it funny how there's always time and money enough to do it WRONG?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3786
    
    1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is my random method not very random?
 
Similar Threads
Creating Multiple Objects?
array to graphic mapping
Thinking in Java's Concurrency chapter: the BankTellerSimulation example
Sublist
How to find repeated value in array?