BlackBerry Forums Support Community
              

Closed Thread
 
LinkBack Thread Tools
Old 11-11-2007, 06:45 AM   #1 (permalink)
New Member
 
Join Date: Oct 2006
Model: 8700
Carrier: Voda
Posts: 10
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Random numbers not random?

Please Login to Remove!

Hello,

possibly a newbie question. I'm using the following method to get random numbers:

public int getRandomInt(int lowerlimit, int upperlimit){
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
// add +1 to include the upper limit
// this is bit shift to right and basicly does that there is no negative numbers so you can take mod
int nr = (generator.nextInt() >>> 1) % (upperlimit +1 - lowerlimit) + lowerlimit;
return nr;
}

However, if I call it more than once in the same run of the app I always get the same "random" number.

Any ideas?

Cheers,

R.
Offline  
Old 11-11-2007, 11:24 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Sep 2004
Model: 8700
Carrier: T-mo
Posts: 162
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's because you are setting the seed to the current millis every time you call the function. So if you call the same function within the same ms, you get the same numbers.

The proper way is create the random generator only once and keep calling nextInt() only.
Offline  
Old 11-11-2007, 06:50 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: TDC
Posts: 115
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by patrickh View Post
That's because you are setting the seed to the current millis every time you call the function. So if you call the same function within the same ms, you get the same numbers.

The proper way is create the random generator only once and keep calling nextInt() only.
i almost second that... only that your first random will always show the same number regardless of what amount of ms used... however, calling the nextInt will initiate a proper random number
Offline  
Closed Thread


Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Audtek WHIRE-KJ 3.5mm 2-Conductor / Mono Keystone Jack
$16.99
Audtek WHIRE-KJ 3.5mm 2-Conductor / Mono Keystone Jack picture:) Southwire Armorlite 25-ft 10/3 Solid Aluminum MC Cable #68584221
$50.0
:) Southwire Armorlite 25-ft 10/3 Solid Aluminum MC Cable #68584221 pictureCarol E1032S.18.10 2 Sound, Alarm and Security Cable, Multi-Conductor, Un... New
$81.99
Carol E1032S.18.10 2 Sound, Alarm and Security Cable, Multi-Conductor, Un... New pictureCerro Wire 138-1602CR-3 Type UF-B 100-Foot 2-Conductor Underground Feeder... New
$68.99
Cerro Wire 138-1602CR-3 Type UF-B 100-Foot 2-Conductor Underground Feeder... New pictureVertical Cable 062-515/S/ST/GY F/UTP Shielded CAT6 Stranded Patch Gray /100ft
$29.99
Vertical Cable 062-515/S/ST/GY F/UTP Shielded CAT6 Stranded Patch Gray /100ft picture






Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.