Java question: Need some sort of global variable.

  • Java
  • Thread starter Jamin2112
  • Start date
  • #1
986
9
Just wondering if the Java experts here could help me. As part of a project I'm making a function rand9() that returns a pseudo-random number 0 through 9. The problem is that each time it's called I need a certain number to increment so that it can generate a new (not necessarily different) random number next time it's called. Hope that makes sense. Just wondering how to approach this. I've checked out http://www.glenmccl.com/tip_002.htm, but I'm not sure how to how to implement that because, at least with JGrasp, I can only have 1 class in a file.
 
Last edited:

Answers and Replies

  • #2
986
9
By the way, I just ran a simulation (using C++ since I suck at Java) and it looks like my random number distributor is pretty uniform.

TOTAL:
# of 0's: 226
# of 1's: 232
# of 2's: 208
# of 3's: 247
# of 4's: 230
# of 5's: 227
# of 6's: 240
# of 7's: 208
# of 8's: 226
# of 9's: 216


EDIT:

Dang it! There's a little repetition. I noticed that a 0 is appearing every 50th character eventually in the output. My formula for the nth number is f(n) = (n * ∏(n)) mod 10.

5, 8, 5, 8, 9, 2, 7, 0, 1, 4, 3, 2, 5, 8, 3, 8, 3, 0, 3, 8, 9, 6, 5, 6, 1, 6, 1, 0, 7, 2, 1, 6, 5, 6, 9, 4, 3, 0, 9, 2, 3, 2, 5, 4, 7, 4, 3, 0, 3, 8, 3, 4, 5, 8, 3, 8, 3, 0, 3, 6, 1, 4, 5, 2, 7, 6, 3, 0, 3, 8, 1, 2, 5, 8, 3, 6, 9, 0, 9, 2, 3, 2, 5, 8, 3, 6, 9, 0, 7, 8, 1, 4, 5, 8, 3, 8, 7, 0, 7, 4, 9, 6, 5, 2, 9, 4, 1, 0, 7, 6, 1, 6, 5, 6, 1, 6, 7, 0, 1, 6, 1, 2, 5, 6, 3, 2, 3, 0, 9, 6, 3, 8, 5, 4, 1, 6, 3, 0, 1, 2, 9, 8, 5, 4, 1, 6, 1, 0, 7, 2, 9, 8, 5, 6, 3, 2, 3, 0, 7, 6, 1, 4, 5, 8, 7, 6, 1, 0, 9, 2, 3, 2, 5, 4, 7, 8, 7, 0, 7, 2, 9, 8, 5, 4, 9, 4, 1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, 4, 3, 0, 1, 6, 7, 4, 5, 8, 7, 6, 1, 0, 3, 8, 1, 8, 5, 6, 1, 4, 1, 0, 3, 4,
 
Last edited:
  • #3
104
3
By the way, I just ran a simulation (using C++ since I suck at Java) and it looks like my random number distributor is pretty uniform.
Look at the header files of http://www.boost.org/doc/libs/1_53_0/doc/html/boost_random.html, pick your flavor and recode that with Java. (And pls don't forget to properly credit the inventors of the algorithms).

About the static var issue:
- If your program is single-threaded you already have all pieces,
- BUT don't make that var "public", but add it to your RNG class itself with "private" visibility.

Regards, Solkar
 
  • #4
986
9
(And pls don't forget to properly credit the inventors of the algorithms).
I didn't know it was already invented.
 
  • #5
53
1
Unless the purpose of this is to try implementing an random number generator then I'd just use an off-the-shelf one. For Java something in http://commons.apache.org/proper/commons-math/ would probably work. Note that it also supports synchronization if you want to use it in a multi-threaded environment.

Javadoc of the random generator interface in that library:
http://commons.apache.org/proper/commons-math/javadocs/api-3.2/index.html

As to static variables in Java that example is fine. I am not sure what you mean about about only one class per file being a jGRASP thing (also why jGRASP instead of Eclipse, NetBeans, or even the free version of Intellij?). Having only one top level class per file is just part of the Java language. You just put the Global class in a separate file and then make sure its on the classpath when you build your main class.
 
  • #6
harborsparrow
Gold Member
554
120
I might hide the random number generator in a static function (that returns the random number), and also you need to think about whether multiple threads will be calling the static function. If they are, the static function itself needs to complete before another thread can call it. There is a way to do that, but right off hand, I am not remembering it.
 
  • #7
724
0
I might hide the random number generator in a static function (that returns the random number), and also you need to think about whether multiple threads will be calling the static function. If they are, the static function itself needs to complete before another thread can call it. There is a way to do that, but right off hand, I am not remembering it.
synchronized
 

Related Threads on Java question: Need some sort of global variable.

Replies
18
Views
6K
  • Last Post
Replies
4
Views
711
  • Last Post
Replies
9
Views
6K
Replies
6
Views
853
Replies
5
Views
3K
  • Last Post
Replies
7
Views
3K
Replies
2
Views
1K
Replies
6
Views
4K
Replies
3
Views
3K
Top