• 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

Looking for alternative method to replace math.random and random function in java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any method can use to random an integer from range -2 to 3.
I no want to use the Math.random and Random function in java.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alex ja wrote:
I no want to use the Math.random and Random function in java.


Why don't you want to use Random? Generating random numbers is not as simple as it might sound.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there's quite some alternatives, but none as easy as just using Random. Is this an exercise?

You can reasonably simply implement a seeded random number generator using a xorshift algorithm, but you'd still need to generate a random seed, which is not easy.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that how it's implemented already?
From java.util.Random

 
alex ja
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to do this for my exercise. It required to implement own random method rather than using the built-in function in java ;D
 
alex ja
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Well, there's quite some alternatives, but none as easy as just using Random. Is this an exercise?

You can reasonably simply implement a seeded random number generator using a xorshift algorithm, but you'd still need to generate a random seed, which is not easy.




Is there possible to generate negative and positive number? Because i only able to generate positive number without negative if using seed...
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alex ja wrote:
Is there possible to generate negative and positive number? Because i only able to generate positive number without negative if using seed...


Generate random number.
Generate random boolean
If boolean true, multiply number by -1
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maneesh, that's a Linear Congruential Generator. It doesn't use xorshift. Indeed, note the lack of exclusive-or.

Here's a cool article on xorshift+/xorshift* generators. Winston linked it here the other day: http://xorshift.di.unimi.it/

In short, it recommends xorshift1024*, xorshift128+ or SplitMix64, depending on the state space that you need. xorshift128+ is quite easy to implement in Java, as it only requires two long fields.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given OP's opening sentence, I suspect that a simple
random.nextInt(2 * N) - N
would do.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alex ja wrote:Is there possible to generate negative and positive number? Because i only able to generate positive number without negative if using seed...



PRNGs just generate random bits. How you interpret those bits is up to you. java.util.Random only uses 31 bits to generate an int, which is always non-negative. You can simply take more bits and widen the acceptable range.

To generate a 64 bit signed integer (a simple long), you can just update the seed, and return one of the two longs comprising it. To generate a signed int, simply cast one of the two longs that comprise the seed to int.
 
alex ja
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really understand how to generate a positive and negative integer using seed, anyone willing to guide or explain further ? ;)
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us what you have so far.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People have already told you it is not a good idea to try to implement your own random procedure (it is not called a function). But you have to willy‑nilly. You must therefore find an algorithm and implement that. Have you searched for an algorithm? Did you know there is an algorithm in the Random class? You can use that. You have already been told that is an LCPNG and the documentation tells you where you can find more details.
Stephan has given you links to a different algorithm.
Have you looked at those two sources?

You can scale the number to be within the 6‑element range you mentioned earlier and subtract 2.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 3 things that "clever" people should never do. One is create their own encryption system, one is create their own security system, and the third is create their own random number generator. That's because all 3 of those things have subtleties that being a mere genius don't suffice for. To do them properly you need specific training and in the cases of self-invented encryption and random-number generation systems, a background in the mathematics.

Of the 3, the safest one to DIY is random number generators, since usually a random number generator that isn't truly random may skew results, but won't cause evil invaders to take over your system. The exception to that rule being the random number generator that drives the security system.

The seminal work on random number generators is Donald Knuth's Art of Computer Programming volume 3 - Seminumerical Algorithms. That's one of the best starting points. You get extra credit if you have a radioactive emitter/detector, though.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:There are 3 things that "clever" people should never do. One is create their own encryption system, one is create their own security system, and the third is create their own random number generator. That's because all 3 of those things have subtleties that being a mere genius don't suffice for.


Amen. Have a cow.

As an enthusiastic (but mediocre) bridge player, one of the things I DO know is that very few of the normal PRNGs we use will ever produce every hand that is possible (≈ 5.36 x 10²⁸ or 2⁹⁶ combinations), because their seeds are far too small and their cycles orders of magnitude too short.
Indeed, it'd be interesting to know if many of the gazillions of online poker and blackjack games out there can't be exploited because they use insufficient RNGs. Unlikely, but a punter can dream...

Winston
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic