Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 13th, 2002, 10:52 AM
estrabd's Avatar
estrabd estrabd is offline
o0o.o0o
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: m00n
Posts: 184 estrabd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 48 sec
Reputation Power: 8
Send a message via ICQ to estrabd Send a message via AIM to estrabd Send a message via Yahoo to estrabd
getting Random ints

I built a class to model a six-sided dice. I am trying to build a reliable "roll()" function that will set the visible side according using the Random class in java.util.*.

I can instantiate a Random object, and get the range I want by doing a Random.nextInt(7), however I have observed that when I run this in a loop I do not get the psuedo-random distribution I would expect. Instead I get consectutive results that contain 2,3,4 and sometimes 5 of the SAME number in a row.

Does anyone know how to rememdy this problem? Thanks.

Brett

Reply With Quote
  #2  
Old March 14th, 2002, 11:34 AM
estrabd's Avatar
estrabd estrabd is offline
o0o.o0o
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: m00n
Posts: 184 estrabd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 48 sec
Reputation Power: 8
Send a message via ICQ to estrabd Send a message via AIM to estrabd Send a message via Yahoo to estrabd
Ha! I am answering my own question...

The deal with java.util.Random is that it needs to be created as a class componant, and not inside a method. Otherwise, since it is psuedo random algorithm, you will get some not so random results if you recreate the object every time you call the method.

Code:

package diceGame
import java.util.*;

public class RollableDice extends SixSidedDice
{
  // constructor
  public RollableDice()
  {
    super();
  }

  // command
  public void roll()
  {
    this.setVisibleSide(this.randGenerator.nextInt(6)+1);
  }
  
  // componant
  Random randGenerator = new Random(System.currentTimeMillis());
  
}

Reply With Quote
  #3  
Old March 16th, 2002, 01:19 AM
Keiichi Keiichi is offline
aHVoPw==
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2001
Posts: 1,058 Keiichi User rank is Lance Corporal (50 - 100 Reputation Level)Keiichi User rank is Lance Corporal (50 - 100 Reputation Level)Keiichi User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 15 h 24 m 31 sec
Reputation Power: 9
Quote:
Originally posted by estrabd
Otherwise, since it is psuedo random algorithm, you will get some not so random results if you recreate the object every time you call the method.
Hmm... thanks for reminding me! I almost forgot about that!
I have it in that situation for one of my scripts now Gotta fix that.
__________________
K1

Reply With Quote
  #4  
Old March 30th, 2002, 01:17 AM
aabha aabha is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: India
Posts: 7 aabha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to aabha
u can try this ...

r.nextInt(1000) % 7;

i am sure..u will not have that repetition problems...
if u r still not satisfied with the result try changin the 1000 to 10000...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > getting Random ints


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway