• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

random number code problem

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am working on a code that requires retuening String 1 88% of the time; String 2 10% Str 3 1.9% and String 4 0.1%.
I developed a function:

This code was provided to me.
But the problem is that I always get String 1 back.. sometimes 2 and 3; it never goes to String 4. I tried calling tis fn 100,000 times; but still no use.

Can anyone pls direct me to what the error here is and how I can change it to acheive my result.

Thanks a lot for your time.

-Jay.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just changed the code to random number just generated once as a static variable and calling guassian function everytime the method is called.
It works now..

Thanks for anyone who took the time to look at this.

Thanks,
Jay.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you work out why that original method didn't work? Have you read the API documentation for nextGaussian? Or for the Random constructor?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope you followed up on Campbell's hints. How did you come up with the 1.555, 2.33 and so on?

Here's a different algorithm I've used over the years. To get your chances into whole numbers (avoiding Java floating types like the chicken flu!) I multiplied them all by 10.

On a couple typical 100000 rep runs I got

This works nicely with weights as well as percentages. I used it in games and simulations with many random choices and I didn't have to make them add up to 100.

Oh, and it works with that array of chances in any order.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted by Stan James

How did you come up with the 1.555, 2.33 and so on?

We really ought to let him off the hook; he didn't. Somebody else gave him that code. And what a strange bit of code it is!

The way they got 1.555 was to look up a table of percentage points for the normal distribution, which is a standard mathematical function and when I was at school I had a book of log tables with exactly those percentage points in the back. There is an example here. If you look up 1.555 you get 0.94 approx. So what Jay Ram's informant has done is to take the one-tailed values and use the absolute value so he is only using half the range so 0.94 - 0.5 comes to 0.44 which is 88% of 0.5.
:roll:

If you are going to use the nextGaussian method you should forget the Math.abs bit and use 1.175 for 88%.

The way I would have done it is like thisI tried Jay's technique with the nextGaussian method; if you create a new Random object every time it always seems to print "88%"; if you always use the same Random object, it prints "88%" most of the time, and sometimes "98%" or "99.9" or "100%".

Jay Ram: Have you worked out why you get this problem, and why you have been successful sorting it out?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the background. Usually I figure I can grok arithmetic, but not math.

Your test for .88 then .98 is summing the odds much as I did, but I stuck with ints because I'm a coward around floats. I rather like my array of weights because the numbers from the problem - 88, 10, 1.9, 0.1 - appear in the code. Well, multiplied by 10. Back in my REXX days I had a reusable random chooser called with a list of weights and a list of results. And the results were executable code snippets. Heh heh.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and Stan for your time.
I agree.. It is somebody else's code I am trying to change.. And you are right.. The numbers were taken from the table.

Now, when I put the random number as a static variable, I understand that the number is generated only once and nextGuassian function returns normally distributed double value.

I tried to put my code in a small java program. The numbers come close to what I expect..

Now the problem is when I put it in my application. This code is part of a EJB which is called by another application. The images are returned as per expected except the least percentage one(error string..). We simulated the request for 5000 times and didn't get a single error(which shd have come 5 times for 0.1%). The other percentages work out ok..

My doubt is whether having mulitple threads affect the way static variable works.. Will having 10 thread in an application cause 10 random numbers to be generated instead of just one..

Please advise.

Thanks a lot again.

-Jay.
[ May 03, 2007: Message edited by: Jay Ram ]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.Random does not really create random numbers, but pseudo-random. It may take its seed from the clock in your PC (C Horstmann, G Cornell, Core Java 2 7/e, vol II advanced features, Santa Clara: Sun Microsystems Press (Prentice-Hall) (2004), page 749), and (Random page of the API), repeated calls to Random with the same seed can produce the same sequence of numbers. So if you instantiate Random many times you will get the same result many times.


Not that I can ever get it to work like that!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a change in the no-arg Random constructor going from JDK 1.4 to JDK 5. In 1.4, the API said "Two Random objects created within the same millisecond will have the same sequence of random numbers" while in 5 this changed to "This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor." The code within the method was changed - basically they just increment a counter with every instantiation and add it to System.nanoTime().

As far as I can see it's guaranteed that each seed will be unique using that constructor, unless there's such a huge amount of time + instantiations that numeric overflow occurs and the seed rolls over and eventually catches up with its earlier values. Which would take such a huge amount of time that it's pretty much irrelevant, I think.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor has been changed? No wonder I can never get the same random numbers out of it!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried recompiling with J1.4.2 and got repeats of the same value.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for your time. But my question is
if I declare my random number variable as a static variable, is it going to be called every time or just once.

My class looks like this:



This code is being called from inside an EJB method to return to a client.
Each call to the method is a separate request from the client. Will that make a difference..

Why is my code working in a stand alone java program but not inside the big application. IOs it because of multithreading etc..
If I instantiate my random number with a fixed seed will that make any difference.

Please help.

Thanks for your help.

-Jay.

[ May 04, 2007: Message edited by: Jay Ram ]
[ May 04, 2007: Message edited by: Jay Ram ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That much of your program makes a static variable holding a Random instance. All threads will share the same instance. I couldn't find anything to indicate that Random is threadsafe or not but I wouldn't want to temp fate. Random might have member variables internally that get into race conditions with multiple threads. Peek into the library source code and see how it looks. Worst case, synchronize on it.

Sychronizing always makes us think performance hit, but doing one line oughtta be minimal. Measure to make sure if it worries you.

If I instantiate my random number with a fixed seed will that make any difference.



It will give you reproducible results, at least in one thread. That might be a good thing in testing.
[ May 04, 2007: Message edited by: Stan James ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I copied your method signature and missed that it is already synchronized. Sine the method is static it's syncing on the class, which ought to keep you out of trouble.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stan.
Yes. As you said, my method is synchronized.. I did change it to synchronized to see if it will cure the problem.. But apparently not..

Not sure why my error string is not generated atall..

Anyways, Thanks again..

-Jay.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will it make a difference when the code is deployed in Z/OS environment..
My stand alone java code is run in my local windows nt machine. whereas my application is deployed in Z/OS environment.. Do you think thats what is causing the problem...

Pls bounce yr thoughts..

-Jay.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know, the behavior you report isn't making much sense. I just tested and, like others here, I can observe the 4th option being selected 0.1% of the time, no problem. Perhaps you've got a buggy version of the JDK - what JDK version are you using, exactly? It may also be worthwhile to rewrite the method along the lines suggested in Stan's and Campbell's earlier posts. There's really no need for using nextGaussian() here; it's just needlessly confusing. And if there's a subtle bug in your JDK's implementation of nextGaussian(), you can avoid it by using next() or nextDouble() instead.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After all the confusion, it turned out that the application that was calling mine was not checking for the proper error string; that's why they had been complaing about not getting the last string..

i got this doubt and ran my logs in error only mode and i found that the error string was being returned properly..

Even though I think it as a 'Waste of time' I did learnt some from this. Always check yr logs properly first. Also, asking qns to you guys made me learn more about Random numbers and how it works!

Thanks a lot for your time..

Regards,
jay.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please to be able to help.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic