Results 1 to 11 of 11

Thread: Math.random() not being random?

  1. #1

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Math.random() not being random?

    For some reason, using Math.random() * x will produce a random number at some points of the code, but will simply produce the max number at other points. Could there be something causing this that you don't need to see the code for?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Math.random() not being random?

    Try giving it a seed. I think you can do it when creating a Random Object.
    Code:
    Random(long seed)

  3. #3

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Math.random() not being random?

    Use the RandomNumberGenerator and ONLY use one instance of it. If you only use one instance it will hit every number only once and when every number has been hit, it restarts.

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Math.random() not being random?

    Think it's the Random class. I can't remember the name of it now.

  6. #6
    Member karkas's Avatar
    Join Date
    Sep 2005
    Posts
    39

    Re: Math.random() not being random?

    System_Error can you please post a small sample code utilizing RandomNumberGenerator ?
    Thank you!

    karkas

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Math.random() not being random?

    The RandomNumberGenerator as in the way I put it is simply a Random object. The biggest thing you need to make sure of, is that only one instance is used:

    Code:
    public class Test
    {
       public Test()
       {
           super();
       }
      
       public int getRandom()
       {
           Random r  = new Random();
    
          return r.nextInt();
       }
    }
    That's the bad version. Notice there's an object created EACH time the getRandom method is called. That's not what you want. Look at a better version:

    Code:
    private Random r;
    public class Test
    {
       public Test()
       {
           super();
     
           r = new Random();
       }
      
       public int getRandom()
       {
          return r.nextInt();
       }
    }
    Notice how the Random object is now global. This is so you don't get any 'patterns' in your code, but in reality, there's no way of escaping them. The bad part about the random object is it will NOT hit any number twice until all numbers have been 'hit' at least once. That's obviously not random since the odds of getting the same number twice is still there. A better way is the utilize random things on a computer such a clock. I've even heard some people using frequencies of sound and such.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Math.random() not being random?

    He posted the same question in VB.NET forum and there the "RandomNumberGenerator" is just like (java.security.SecureRandom) to Java. but what do I know
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9
    Member karkas's Avatar
    Join Date
    Sep 2005
    Posts
    39

    Re: Math.random() not being random?

    System_Error and ComputerJy thank you for your replies. I'm interested in creating a routine that gives me a true random number (not using the pseudo-random generator e.g. rnd() in VB). I read somewhere that using
    System.Security.Cryptography.RandomNumberGenerator will do the trick but I just don't know how to do that. I haven't decided on the language yet so Java is fine (personally I will like to use VB .net 2005).

    Regards

    karkas

  10. #10
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Math.random() not being random?

    There's no such thing as a true random number. If you want something close then use sound card or video card frequencies or something.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Math.random() not being random?

    Well, there are hardware random number generators that use radioactive decay. Those are truly random
    So are detectors for background radiation and similar natural whitenoise listeners.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width