Results 1 to 10 of 10
  1. #1
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default Random Generator

    Ok little bit of confusion with a Stanford University CS106a lesson on using the random generator.

    Mehran Sahami uses the following code to create a private instance variable of type RamdomGenerator -- hope I said that right.

    Java Code:
    private RandomGenerator rgen = RandomGenerator.getInstance();
    Anyway, for me that simply doesn't work whereas the following does:

    Java Code:
    private RandomGenerator rgen = new RandomGenerator();
    Are both codes doing the same thing? Is there any reason why the other code wouldn't work for me? I mean the only difference I could think of is that I'm using a Mac whereas he was on a PC, but that shouldn't make a different right? The impression I got is that programming in Java on a Mac and programming in Java on a PC are exactly the same since they both get translated by the JVM anyway or am I mistaken?

    Thanks!

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Were there any errors, or did the code compile but not function as intended? From what I googled, the RandomGenerator class is part of the acm package, that isn't standard Java.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    You're right it isn't standard Java. But even the ACM documentation uses that code so I'm wondering why it doesn't work for me. I get the following error:

    "The method getInstance() is undifined for the type RandomGenerator"

    Is it possible that there are different versions of the ACM library or something and I have one that is out of date? What's even more confusing is that the book "The Art and Science of Java" doesn't use that code either, instead it uses the "= new RandomGenerator()" code as I do :confused:

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,444
    Rep Power
    25

    Default

    I just decompiled my version of the above class and found:
    Java Code:
        public static RandomGenerator getInstance()
    The class file from the acm.jar is dated 8/25/06

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    I thought that Stanford course uses Eclipe; when you type "RandomGenerator.", Eclipse shows a list of fields and methods present in the RandomGenerator class. I don't think any confusion will be left ...

    kind regards,

    Jos

  6. #6
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Ok not to worry then.

    It's just that the video lecture taught one way but the book taught another way... the way the book taught works fine for me so I guess it's all good. Just wanted to know why they were different and whether it would be a problem; but I guess it shouldn't.

    Thanks

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,444
    Rep Power
    25

    Default

    One consideration is that the static method creates one object and returns that on future calls vs creating a new one every time you need one. Don't know how the seed is set. Does a new object always return the same series of numbers? where using the existing one would not.

  8. #8
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I ran the following:

    Java Code:
    import acm.program.*;
    import acm.util.*;
    
    public class Test extends ConsoleProgram {
    
    	public void run() {
    		
    	random.setSeed(2);	
    
    	println (+random.nextInt());
    	println (+random.nextInt());
    	println (+random.nextInt());	
    
    	}
    	
    	private RandomGenerator random = new RandomGenerator();
    
    }
    And yes I do get the same sequence of numbers:

    -1154715079
    1260042744
    -423279216

    each time the program is run.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,444
    Rep Power
    25

    Default

    Use a pseudo random seed by using the system's current time. That'll give you a different sequence.

  10. #10
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

Similar Threads

  1. Random Numbers generator
    By pinkdreammsss in forum Java Applets
    Replies: 10
    Last Post: 04-08-2010, 03:38 AM
  2. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM
  3. Mersenne twister random generator
    By mental in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:21 AM
  4. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 02:06 AM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 PM

Posting Permissions

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