best approach to generate random number in java

Discussion in 'Java' started by Matt, Oct 13, 2004.

  1. Matt

    Matt Guest

    If I tried Random with seed, my observation is it always generate the
    same number with seed:

    Approach #1
    ===========
    Random generator = new Random(19580427);
    int r = Math.abs(generator.nextInt()) % 10000;

    Approach #2
    ===========
    If I do it without a seed, it can pretty much generate different
    number from [0-9999] each time.

    Random generator = new Random();
    int r = Math.abs(generator.nextInt()) % 10000;

    So what's the purpose of using a seed? I thought using a seed can
    yield a more random number. Whats the best approach to generate random
    number in java?

    Please advise. thanks!!
     
    Matt, Oct 13, 2004
    #1
    1. Advertising

  2. Matt

    Paul Lutus Guest

    Matt wrote:

    > If I tried Random with seed, my observation is it always generate the
    > same number with seed:


    That is how it is supposed to work. Pseudorandom number generators are
    supposed to create a series of numbers that are random with respect to each
    other, but the sequence can be recreated exactly if the same seed is used.
    This is a Good Thing(tm).

    You know, you can always read the docmentation.

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html

    http://www.cafeaulait.org/course/week4/54.html

    / ...

    > So what's the purpose of using a seed?


    See above. It allows you to recreate the same sequence.

    > I thought using a seed can
    > yield a more random number.


    "More random". Hmm. Entirely new concept.

    > Whats the best approach to generate random
    > number in java?


    You already have a pretty good one. Why not say what you want, instead of
    what you are not understanding?

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 13, 2004
    #2
    1. Advertising

  3. "Matt" <> wrote in message
    news:...
    > If I tried Random with seed, my observation is it always generate

    the
    > same number with seed:
    >


    That's the intended behavior of seed. Sometimes you need to get the
    same series over and over, especially for testing. Either use the
    default behavior you described or use something like the system time
    to make your own seed (which is really doing the same thing as the
    default behavior).

    The important behavior is that the series of numbers generated is not
    correlated in any obvious way with the seed value. This is an
    important property of pseudo-random number generators.

    If you need true "random numbers" there is HW to do it, exploiting the
    behavior of certain electronic devices.

    --arne
     
    arne thormodsen, Oct 13, 2004
    #3
  4. Matt

    Alex Hunsley Guest

    Matt wrote:
    > If I tried Random with seed, my observation is it always generate the
    > same number with seed:
    >
    > Approach #1
    > ===========
    > Random generator = new Random(19580427);
    > int r = Math.abs(generator.nextInt()) % 10000;
    >
    > Approach #2
    > ===========
    > If I do it without a seed, it can pretty much generate different
    > number from [0-9999] each time.
    >
    > Random generator = new Random();
    > int r = Math.abs(generator.nextInt()) % 10000;
    >
    > So what's the purpose of using a seed? I thought using a seed can
    > yield a more random number. Whats the best approach to generate random
    > number in java?
    >
    > Please advise. thanks!!


    You actually have it the wrong way round - seeding a random number
    generator makes it *less* random seeming, because when you seed a random
    number generator, the sequence of "random"[1] numbers that follow will
    always be the same for that seed.

    This is useful behaviour in some situations. Say, for instance, your
    program has random behaviour, but you need to debug a certain situation
    that crops up, and have everything the same each time you run it: you
    would temporarily put in some code to seed the random number generator
    each time the program ran with the same number. This way the programs
    execution would be identical each time you ran it for that debugging.



    [1] I put "random" in quotes because random number generators are
    usually in reality pseudo-random number generators - they use an
    algorithm that gives the impression of randomness (whilst being purely
    deterministic, i.e. follows rules.).


    alex
     
    Alex Hunsley, Oct 13, 2004
    #4
  5. "Paul Lutus" <> wrote in message
    news:...
    > Matt wrote:
    >
    > > If I tried Random with seed, my observation is it always generate the
    > > same number with seed:

    >
    > That is how it is supposed to work. Pseudorandom number generators are
    > supposed to create a series of numbers that are random with respect to

    each
    > other, but the sequence can be recreated exactly if the same seed is used.
    > This is a Good Thing(tm).
    >
    > You know, you can always read the docmentation.
    >
    > http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
    >
    > http://www.cafeaulait.org/course/week4/54.html
    >
    > / ...
    >
    > > So what's the purpose of using a seed?

    >
    > See above. It allows you to recreate the same sequence.
    >
    > > I thought using a seed can
    > > yield a more random number.

    >
    > "More random". Hmm. Entirely new concept.


    The quality of randomness as applied to PRGs can be assessed statistically.
    Some PRGs give results with better distributions than others. My
    understanding is that for Java the SecureRandom number generator gives
    longer periods and better distributions than Random.

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Oct 13, 2004
    #5
  6. Matt

    Paul Lutus Guest

    Matt Humphrey wrote:

    >
    > "Paul Lutus" <> wrote in message
    > news:...
    >> Matt wrote:
    >>
    >> > If I tried Random with seed, my observation is it always generate the
    >> > same number with seed:

    >>
    >> That is how it is supposed to work. Pseudorandom number generators are
    >> supposed to create a series of numbers that are random with respect to

    > each
    >> other, but the sequence can be recreated exactly if the same seed is
    >> used. This is a Good Thing(tm).
    >>
    >> You know, you can always read the docmentation.
    >>
    >> http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
    >>
    >> http://www.cafeaulait.org/course/week4/54.html
    >>
    >> / ...
    >>
    >> > So what's the purpose of using a seed?

    >>
    >> See above. It allows you to recreate the same sequence.
    >>
    >> > I thought using a seed can
    >> > yield a more random number.

    >>
    >> "More random". Hmm. Entirely new concept.

    >
    > The quality of randomness as applied to PRGs can be assessed
    > statistically.
    > Some PRGs give results with better distributions than others. My
    > understanding is that for Java the SecureRandom number generator gives
    > longer periods and better distributions than Random.


    Yes, all fine, but my point is that "random" is a superlative term.
    Therefore saying "more random" is like saying "more perfect".

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 13, 2004
    #6
  7. "Paul Lutus" <> wrote in message
    news:...
    > Matt Humphrey wrote:
    >
    > >
    > > "Paul Lutus" <> wrote in message
    > > news:...
    > >> Matt wrote:
    > >>
    > >> > If I tried Random with seed, my observation is it always generate the
    > >> > same number with seed:
    > >>
    > >> That is how it is supposed to work. Pseudorandom number generators are
    > >> supposed to create a series of numbers that are random with respect to

    > > each
    > >> other, but the sequence can be recreated exactly if the same seed is
    > >> used. This is a Good Thing(tm).
    > >>
    > >> You know, you can always read the docmentation.
    > >>
    > >> http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
    > >>
    > >> http://www.cafeaulait.org/course/week4/54.html
    > >>
    > >> / ...
    > >>
    > >> > So what's the purpose of using a seed?
    > >>
    > >> See above. It allows you to recreate the same sequence.
    > >>
    > >> > I thought using a seed can
    > >> > yield a more random number.
    > >>
    > >> "More random". Hmm. Entirely new concept.

    > >
    > > The quality of randomness as applied to PRGs can be assessed
    > > statistically.
    > > Some PRGs give results with better distributions than others. My
    > > understanding is that for Java the SecureRandom number generator gives
    > > longer periods and better distributions than Random.

    >
    > Yes, all fine, but my point is that "random" is a superlative term.
    > Therefore saying "more random" is like saying "more perfect".


    I agree that the lay definition of random as meaning without design or
    arbitrary is superlative, but when speaking of random number generators
    there are many grades of randomness (e.g. perfectly, truly) and so in this
    context the term is not superlative.

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Oct 13, 2004
    #7
  8. Matt

    Paul Lutus Guest

    Matt Humphrey wrote:

    / ...

    >> Yes, all fine, but my point is that "random" is a superlative term.
    >> Therefore saying "more random" is like saying "more perfect".

    >
    > I agree that the lay definition of random as meaning without design or
    > arbitrary is superlative, but when speaking of random number generators
    > there are many grades of randomness (e.g. perfectly, truly) and so in this
    > context the term is not superlative.


    Even here, random means random, and in the past I have been called on using
    "random" to mean sort of random, many times. But your point is appropriate
    for everyday use.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 13, 2004
    #8
  9. (Matt) writes:

    > If I do it without a seed, it can pretty much generate different
    > number from [0-9999] each time.
    >
    > Random generator = new Random();
    > int r = Math.abs(generator.nextInt()) % 10000;


    Check the source: This isn't without a seed, but with a "default" seed
    equal to the value of System.currentTimeMillis().

    > So what's the purpose of using a seed?


    As others have pointed out, to recreate a given pseudorandom sequence.
    Useful for encryption, storing randomly generated game maps as a long
    etc.
     
    Tor Iver Wilhelmsen, Oct 13, 2004
    #9
  10. Matt

    Alex Hunsley Guest

    Paul Lutus wrote:
    > Matt Humphrey wrote:
    >
    > / ...
    >
    >
    >>>Yes, all fine, but my point is that "random" is a superlative term.
    >>>Therefore saying "more random" is like saying "more perfect".

    >>
    >>I agree that the lay definition of random as meaning without design or
    >>arbitrary is superlative, but when speaking of random number generators
    >>there are many grades of randomness (e.g. perfectly, truly) and so in this
    >>context the term is not superlative.

    >
    >
    > Even here, random means random, and in the past I have been called on using
    > "random" to mean sort of random, many times. But your point is appropriate
    > for everyday use.


    Random, in the context of computer programming, is generally accepted as
    meaning pseudo-random (whether or not it should be is another matter). I
    suppose things are this way because truly random sequences require
    something non-alorithmic such as RNG hardware which usually isn't there
    in most cases.
    So it's perfectly clear, to me anyway, what someone means in this
    context by "more random", and IMHO it's quite acceptable.
    In everyday life (or perhaps mathematics), "more random" wouldn't make
    much sense.
     
    Alex Hunsley, Oct 13, 2004
    #10
  11. Matt

    Alex Hunsley Guest

    Alex Hunsley wrote:
    > Paul Lutus wrote:
    >
    >> Matt Humphrey wrote:
    >>
    >> / ...
    >>
    >>
    >>>> Yes, all fine, but my point is that "random" is a superlative term.
    >>>> Therefore saying "more random" is like saying "more perfect".
    >>>
    >>>
    >>> I agree that the lay definition of random as meaning without design or
    >>> arbitrary is superlative, but when speaking of random number generators
    >>> there are many grades of randomness (e.g. perfectly, truly) and so in
    >>> this
    >>> context the term is not superlative.

    >>
    >>
    >>
    >> Even here, random means random, and in the past I have been called on
    >> using
    >> "random" to mean sort of random, many times. But your point is
    >> appropriate
    >> for everyday use.

    >
    >

    [snip me]
    > ...I suppose things are this way because truly random sequences
    > require something non-alorithmic such as RNG hardware which usually
    > isn't there in most cases...


    Actually, I think I am wrong there!
    I can easily write an alogrithm that produces very random digits: the
    digits of pi, or e, etc...
    I suppose what I should say is "truly random sequences cannot be
    generated by algorithms *in constant runtime per generation*".

    alex
     
    Alex Hunsley, Oct 13, 2004
    #11
  12. >> ...I suppose things are this way because truly random sequences require
    >> something non-alorithmic such as RNG hardware which usually
    > > isn't there in most cases...

    >
    > Actually, I think I am wrong there!
    > I can easily write an alogrithm that produces very random digits: the
    > digits of pi, or e, etc...


    I think you were right. Numbers of pi or e are *not* random... well, it (of
    course) depends on the meaning of random. According to the first dictionary
    I found googling (hyperdictionary), random is a synonyme for
    "unpredictable". This looks good enough to me.
    The numbers of pi are definitly predictable. Unpredictable implies that it
    does not follow any algorithm.

    --
    Vincent
     
    Vincent Lascaux, Oct 13, 2004
    #12
  13. Matt

    Paul Lutus Guest

    Vincent Lascaux wrote:

    >>> ...I suppose things are this way because truly random sequences require
    >>> something non-alorithmic such as RNG hardware which usually
    >> > isn't there in most cases...

    >>
    >> Actually, I think I am wrong there!
    >> I can easily write an alogrithm that produces very random digits: the
    >> digits of pi, or e, etc...

    >
    > I think you were right. Numbers of pi or e are *not* random... well, it
    > (of course) depends on the meaning of random. According to the first
    > dictionary I found googling (hyperdictionary), random is a synonyme for
    > "unpredictable". This looks good enough to me.
    > The numbers of pi are definitly predictable. Unpredictable implies that it
    > does not follow any algorithm.


    Yes, that adequately muddies the water. :) But "random" doesn't necessarily
    mean "unpredictable." It may instead mean evenly distributed, with no
    pattern.

    The digits of Pi are deterministic, but they are also random. As to the
    first, they can be reliably and repeatably generated using a relativelty
    small algorithm. As to the second, the relation between internal sets of
    digits is quite random, and at many scales.

    Stephen Wolfram (Mathematica) has developed a particular cellular automaton
    (rule 30) that he claims produces a very highly random number sequence. In
    his recent book "A New Kind of Science" he shows patterns in the output of
    most pseudorandom number generators (that's a "bad thing"(tm)), and the
    output of his method is apparently much superior, despite being
    conceptually very simple.

    http://mathworld.wolfram.com/ElementaryCellularAutomaton.html

    My point? Very predictable, but very random.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 13, 2004
    #13
  14. Matt

    Tom McGlynn Guest

    Paul Lutus <> wrote in message
    ....
    > Yes, all fine, but my point is that "random" is a superlative term.
    > Therefore saying "more random" is like saying "more perfect".


    Surely a legal usage at least in the US where it is enshrined in the
    Constitution...
     
    Tom McGlynn, Oct 14, 2004
    #14
  15. Matt

    Paul Lutus Guest

    Tom McGlynn wrote:

    > Paul Lutus <> wrote in message
    > ...
    >> Yes, all fine, but my point is that "random" is a superlative term.
    >> Therefore saying "more random" is like saying "more perfect".

    >
    > Surely a legal usage at least in the US where it is enshrined in the
    > Constitution...


    Yes, one can't argue with that. :)

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 14, 2004
    #15
  16. Matt wrote:
    >
    > Random generator = new Random();
    > int r = Math.abs(generator.nextInt()) % 10000;
    >


    Not directly related to your question - but if you want to generate a
    random number between 0 and 10000, taking the modulus is *not* the best
    way. Use generator.nextInt(10000) instead. Refer to the API docs of that
    method to see the explanation why.

    BK
     
    Babu Kalakrishnan, Oct 14, 2004
    #16
  17. Matt

    Alex Hunsley Guest

    Vincent Lascaux wrote:
    >>>...I suppose things are this way because truly random sequences require
    >>>something non-alorithmic such as RNG hardware which usually
    >>>isn't there in most cases...

    >>
    >>Actually, I think I am wrong there!
    >>I can easily write an alogrithm that produces very random digits: the
    >>digits of pi, or e, etc...

    >
    >
    > I think you were right. Numbers of pi or e are *not* random... well, it (of
    > course) depends on the meaning of random. According to the first dictionary
    > I found googling (hyperdictionary), random is a synonyme for
    > "unpredictable". This looks good enough to me.
    > The numbers of pi are definitly predictable.


    I disagree - I would say they're not!

    Suppose you know that PI is being used.
    You see the consecutive digits 1, 2, 3 come out of the generator.

    Q: What is the next number?

    A: You don't know. The seqeunce 1, 2, 3 occurs many times, even in the
    first few million places of pi.

    In other words, this is an unpredictable sequence. Seeing a certain set
    of digits that come out of the pi generator doesn't tell you from where
    in pi those digits came from, or what the next digit will be.


    > Unpredictable implies that it
    > does not follow any algorithm.


    Given this business with pi, I believe that the meaning of unpredictable
    is a bit more fuzzy than that...
    The digits of pi, from the first decimal place onwards, are predictable.
    The digits coming out of a pi random number generator are not
    predictable, IMHO, as you don't know from whence in pi the digits you
    are seeing come from.
    Even if the random number generator produced the digits

    14159265358979

    which happen to be the first decimal places of pi, you wouldn't know the
    next numbers, as these digits it produced could also have come from
    somewhere very deep in pi.... you don't, can't, know. The expansion of
    pi can contain repetitions of previous number sequences - it's just that
    in general the sequence does not follow a pattern.

    alex
     
    Alex Hunsley, Oct 14, 2004
    #17
  18. Matt

    Alex Hunsley Guest

    Paul Lutus wrote:
    > Vincent Lascaux wrote:
    >
    >
    >>>>...I suppose things are this way because truly random sequences require
    >>>>something non-alorithmic such as RNG hardware which usually
    >>>>isn't there in most cases...
    >>>
    >>>Actually, I think I am wrong there!
    >>>I can easily write an alogrithm that produces very random digits: the
    >>>digits of pi, or e, etc...

    >>
    >>I think you were right. Numbers of pi or e are *not* random... well, it
    >>(of course) depends on the meaning of random. According to the first
    >>dictionary I found googling (hyperdictionary), random is a synonyme for
    >>"unpredictable". This looks good enough to me.
    >>The numbers of pi are definitly predictable. Unpredictable implies that it
    >>does not follow any algorithm.

    >
    >
    > Yes, that adequately muddies the water. :) But "random" doesn't necessarily
    > mean "unpredictable." It may instead mean evenly distributed, with no
    > pattern.


    I think this is what I was saying in my very recent post just now, but
    using different words :)


    I have a theorem: (if it can be called that)

    Truly unpredictable sequences of numbers can be produced
    by algorithmic means, but the runtime needed to produce
    successive numbers ramps up (possibly exponentially).

    I think I might ask about this in comp.theory.

    alex
     
    Alex Hunsley, Oct 14, 2004
    #18
  19. Matt

    Paul Lutus Guest

    Alex Hunsley wrote:

    / ...

    > I have a theorem: (if it can be called that)
    >
    > Truly unpredictable sequences of numbers can be produced
    > by algorithmic means,


    Umm, IMHO no. Because if one has access to the algorithm, and if one sets it
    to the same initial state, it will produce the same sequence. Therefore the
    original algorithm can be used to "predict" the entire sequence.

    If one does not have access to the original algorithm, one might embark on a
    quest to locate the algorithm, using, say, a future quantum computer
    capable of producing algorithms for supplied number sequences.

    Then, if the quantum computer approach were to fail, strongly implying that
    the number sequence was in fact nondeterministic, one could fairly say it
    was unpredictable and very complex, e.g. using the classical definition
    that the number sequence is itself the shortest expression of the
    information it contained.

    > but the runtime needed to produce
    > successive numbers ramps up (possibly exponentially).


    I think there is an interesting (and recognized) point after which the
    numbers themselves are the shortest form the information can take. That is
    truly unpredictable.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 14, 2004
    #19
  20. Matt

    Alex Hunsley Guest

    Paul Lutus wrote:
    > Alex Hunsley wrote:
    >
    > / ...
    >
    >
    >>I have a theorem: (if it can be called that)
    >>
    >> Truly unpredictable sequences of numbers can be produced
    >> by algorithmic means,

    >
    >
    > Umm, IMHO no. Because if one has access to the algorithm, and if one sets it
    > to the same initial state, it will produce the same sequence. Therefore the
    > original algorithm can be used to "predict" the entire sequence.


    There is a difference between the determinism of an algorithm and the
    predictability of the distribution of the numbers it produces...

    Also, I am assuming the user does not know where the starting point in
    the sequence is. The user does not know the seed - and if they do not
    know this, then it doesn't matter that they know it is the pi RNG.
    (Of course, the above only truly holds if the starting position could be
    anywhere in pi, despite the fact that the further along the starting
    postiion, the s l o w e r the algorithm would run. But I am talking of
    theoretics here, not practicalities.)

    >
    > If one does not have access to the original algorithm, one might embark on a
    > quest to locate the algorithm, using, say, a future quantum computer
    > capable of producing algorithms for supplied number sequences.


    If you don't know the starting point in the sequence, knowing the
    algorithm wouldn't matter - you have no idea when the sequence you see
    will diverge from what might look like a 'familiar' sequence like
    14159265358979.

    > Then, if the quantum computer approach were to fail, strongly implying that
    > the number sequence was in fact nondeterministic, one could fairly say it
    > was unpredictable and very complex, e.g. using the classical definition
    > that the number sequence is itself the shortest expression of the
    > information it contained.


    Unfortunately, this scheme wouldn't always work. For the computer to
    guess the pi algorithm, you would have to

    a) put in all the decimal places of pi (!)
    b) leave the computer to run forever (!)

    These requirements scupper the plan.... no matter how many qubits you
    put in a quantuum computer, infinity still poses a problem.

    There's no reason that the scheme shouldn't work for finite sequences
    though.

    >> but the runtime needed to produce
    >> successive numbers ramps up (possibly exponentially).

    >
    >
    > I think there is an interesting (and recognized) point after which the
    > numbers themselves are the shortest form the information can take. That is
    > truly unpredictable.


    Yup, this is a good point. Despite its seeming randomness, the places of
    pi are deterministic, and reproducable with a very short algorithm, so
    in that sense the places of pi are not utterly random. Which is why it's
    so fascinating.
    A well known rough test of how random information is: zip it up in
    winzip. Now, I wonder what compression you get if you zip up a text file
    containing places of pi?

    It reminds me of the whole mandelbrot set thang: how such an amazing
    structure is encoding by an eqn. as simple as z |-> z^2 + c (and knowing
    how complex numbers work).
    ((I have started reading "The Road to Reality" by Roger Penrose which
    touches on this theme at the beginning. Highly recommended book, if all
    the maths doesn't put a spade in your head.))

    Another digression:
    I wonder what percentage of all possible permutations of information are
    truly random and non-compressible?
    For example, generate all possible binary strings of length 100. (So
    you'd have 2^100 strings to test.) For each string, see how compressible
    it is. What percentage of them will be not compressible at all? Will
    this remain constant for different sizes of strings? (answer: no, I feel.)

    alex
     
    Alex Hunsley, Oct 15, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RayKnight

    Generate a random number

    RayKnight, Apr 29, 2005, in forum: Java
    Replies:
    2
    Views:
    328
    Peter MacMillan
    Apr 29, 2005
  2. Acacia

    Generate a random number

    Acacia, Aug 25, 2003, in forum: C++
    Replies:
    6
    Views:
    551
    Kevin Goodsell
    Aug 26, 2003
  3. Wahoo
    Replies:
    4
    Views:
    465
    Jerry Coffin
    May 18, 2004
  4. globalrev
    Replies:
    4
    Views:
    774
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,180
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page