Results 1 to 10 of 10
Thread: Random Generator
- 05-30-2010, 06:12 PM #1
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
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.
Anyway, for me that simply doesn't work whereas the following does:Java Code:private RandomGenerator rgen = RandomGenerator.getInstance();
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?Java Code:private RandomGenerator rgen = new RandomGenerator();
Thanks!
- 05-30-2010, 06:37 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 05-30-2010, 07:05 PM #3
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
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:
- 05-30-2010, 08:14 PM #4
I just decompiled my version of the above class and found:
The class file from the acm.jar is dated 8/25/06Java Code:public static RandomGenerator getInstance()
- 05-30-2010, 08:39 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-30-2010, 10:08 PM #6
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
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
- 05-30-2010, 11:46 PM #7
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.
- 05-31-2010, 07:00 PM #8
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
I ran the following:
And yes I do get the same sequence of numbers: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(); }
-1154715079
1260042744
-423279216
each time the program is run.
- 05-31-2010, 07:13 PM #9
Use a pseudo random seed by using the system's current time. That'll give you a different sequence.
- 05-31-2010, 08:15 PM #10
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
Similar Threads
-
Random Numbers generator
By pinkdreammsss in forum Java AppletsReplies: 10Last Post: 04-08-2010, 03:38 AM -
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04-02-2010, 06:47 PM -
Mersenne twister random generator
By mental in forum New To JavaReplies: 6Last Post: 03-23-2010, 02:21 AM -
Random Shape generator
By scheng12 in forum New To JavaReplies: 1Last Post: 03-09-2009, 02:06 AM -
Random Generator
By padutch2 in forum New To JavaReplies: 1Last Post: 12-03-2007, 06:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks