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.
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.
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.
Sometimes the only way things ever got fixed is because people became uncomfortable.
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.
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...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad: