addressalign-toparrow-leftarrow-leftarrow-right-10x10arrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1languagelaunch-new-window--smalllight-bulblightning-boltlinklocation-pinlockm-swarmSearchmailmediummessagesminusmobilemoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstar-shapestartickettrashtriangle-downtriangle-uptwitteruserwarningyahooyoutube

Re: [ljc] A little Friday morning challenge for the group...

From: Ben E.
Sent on: Tuesday, January 29, 2013, 7:42 PM
Coming rather late to this thread (I've not been well).

There's a couple of points that are worth distilling out here.

Firstly, we need to slightly special-case the problem, based on what
to do with negative numbers, and when crossing the zero in such a way
that dist(min, max) > Integer.MAX_VALUE - this is annoying detail but
needs to be handled properly.

Rather more importantly, I don't think anyone's touched on the
question of what this pseudo-random number generator (PRNG) is *for*.

This matters, probably more than any other concern about
implementation. Not all use cases of "random" numbers are equal.

For most cases that anyone other than a specialist will encounter, a
relatively straightforward implementation, such as a Linear
Congruential Generator will do. This uses purely integer maths, and
can be implemented directly - and the implementation in Random.next()
is in fact of LCG type, and rather straightforward.

The issue with these is that they still display unfortunate
correlation behaviour - which makes them not suitable for usage in
circumstances which require high-quality randomness. Monte Carlo
simulations and certain financial trading applications are in this
category of requiring better randomness. Algorithms based on discrete
maths using Mersenne primes are often used for these purposes instead
(although they are still not usually considered of high enough quality
for use in serious crypto applications).

Finally, in certain jurisdictions where gambling is legal, there are
statutes covering the quality of random numbers that may be used in a
gambling machine or program. You could go to jail for using the wrong
PRNG in a Vegas application!

So - what's the application you need these pseudorandom numbers for, Wes?

Cheers,

Ben



On Fri, Jan 25, 2013 at 11:01 AM, Wesley Hall <[address removed]> wrote:
> Hello folks,
>
> I came across what turned out to be a pretty interesting problem on a
> project of mine last night, and I thought I would share it with you
> guys. I am interested to see if anyone can come up with a better
> solution than I did (highly likely I suspect :)).
>
> Your mission, should you choose to accept it, is to write a method, in
> Java, with the following signature...
>
> public static long generateRandom(long min, long max) {
>    // code here
> }
>
> Such that the method generates a random long value that is between the
> two parameters provided (inclusive). The only restriction on the
> parameters is that max >= min, for brevity you can assume this is
> ensured outside of the method and that you don't need to check inside.
>
> The first person to provide a working solution gets to look like a
> hero... and if you provide a solution that I think is better than the
> one I have, then I will (with your permission), use this version in
> the library that I am about to open source and give you contributor
> credit.
>
> GO....
>
> Wes
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
> https://www.meetup...­
> This message was sent by Wesley Hall ([address removed]) from LJC - London Java Community.
> To learn more about Wesley Hall, visit his/her member profile: https://www.meetup...­
> Set my mailing list to email me
>
> As they are sent
> https://www.meetup...­
>
> In one daily email
> https://www.meetup...­
>
> Don't send me mailing list messages
> https://www.meetup...­
> Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]
>

People in this
group are also in: