LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2006, 10:56 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
random java number?!?!?!


hello all im trying to print some random numbers between 0 and 16777215 in java when i press a button, i have this method at the moment:

Code:
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    Random rand = new Random();
    int test = rand.nextInt(16777215 ) + 1;
    print( String.valueOf( rand ) );
    }

but when i press the button it prints information like this:

Code:
java.util.Random@1c8efd1
java.util.Random@f892a4
java.util.Random@1636e4e
java.util.Random@df0438
java.util.Random@18e261d
java.util.Random@1684706
java.util.Random@10efd7c
java.util.Random@12412e7
java.util.Random@1552b76
java.util.Random@1ab11b0
java.util.Random@165b7e
java.util.Random@1d0d124
java.util.Random@52c6b4
java.util.Random@738d08
java.util.Random@302e67
java.util.Random@136a1a1
java.util.Random@1ad6b4b
java.util.Random@5f2db0
java.util.Random@b0a3f5

WTF???? please help, trscookie
 
Old 07-18-2006, 11:25 AM   #2
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
How about:
Code:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
   Random rand = new Random();
   int test = rand.nextInt(16777215 ) + 1;
   print( String.valueOf( test ) );
}
You were printing the wrong variable.
 
Old 07-18-2006, 12:10 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
I like to seed my random numbers when I create them too. You can do this easily w/: Random rand = new Random(System.currentTimeMillis());
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Random number generation jacques83 Programming 5 02-04-2006 12:38 PM
java- static messing with random number generation titanium_geek Programming 8 10-15-2005 01:39 PM
Random number generators blufire Programming 5 11-09-2004 06:02 PM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration