random nextInt not working properly
Author |
Message |
goroyoshi
![](http://compsci.ca/v3/uploads/user_avatars/13875868514dae1bcc8da17.png)
|
Posted: Sun Jan 15, 2012 8:32 pm Post subject: random nextInt not working properly |
|
|
When I use nextInt, it keeps repeating after the first go, ex: it'll go 10 50 50 50 50
it works perfectly fine with my friend's computer, the only difference is that he is using dr. java and i'm using rtp
Java: |
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Random;
import java.io.*;
import hsa.Console;
class FallingLetters extends JFrame
{
char choice, difficulty, test;
int score;
Console c;
public FallingLetters ()
{
c = new Console ("Falling Letters");
getContentPane (). add (new JTextField ());
KeyboardFocusManager manager = KeyboardFocusManager. getCurrentKeyboardFocusManager ();
manager. addKeyEventDispatcher (new KeyDispatch ());
}
private class KeyDispatch implements KeyEventDispatcher
{
public boolean dispatchKeyEvent (KeyEvent e )
{
if (e. getID () == KeyEvent. KEY_PRESSED)
{
test = e. getKeyChar ();
}
return false;
}
}
public void game ()
{
title ();
Letter [] a = new Letter [50];
int letters = 0, score = 0;
Random r = new Random ();
char [] ex = new char [50];
for (int x = 0 ; x < a. length ; x++ )
{
ex [x ] = (char ) (r. nextInt (26) + 97);
if (difficulty == '1')
{
a [x ] = new Letter (c, ex [x ], 2, 0);
}
else if (difficulty == '2')
{
a [x ] = new Letter (c, ex [x ], 1, 500);
}
else
{
a [x ] = new Letter (c, ex [x ], 1, 0);
}
}
c. setCursor (2, 1);
c. println ("Press space in the text field to start.");
while (test != ' ')
{
}
while (letters < 50)
{
c. setCursor (2, 1);
c. println ("Score: " + score + "/50 Progress: " + letters + "/50");
a [letters ]. start ();
// try
// {
// Robot robot = new Robot ();
// robot.keyPress (ex - 32);
// }
// catch (AWTException e)
// {
// }
// try
// {
// if (difficulty == '1')
// Thread.sleep (333);
// else if (difficulty == '2')
// Thread.sleep (250);
// else
// Thread.sleep (166);
// }
// catch (InterruptedException e)
// {
// }
try
{
if (difficulty == '1')
Thread. sleep (920);
else if (difficulty == '2')
Thread. sleep (690);
else
Thread. sleep (450);
}
catch (InterruptedException e )
{
}
if (test == ex [letters ])
{
score++;
a [letters ]. letterGot ();
}
test = ' ';
letters++;
}
System. out. println (score );
}
public void intro ()
{
title ();
c. println ("This program will display falling letters and you must type them in time to get points.");
pauseProgram ("continue");
}
public void mainMenu ()
{
title ();
c. println ("Press 1 to continue.");
c. println ("Press 2 to exit.");
c. print ("What would you like to do?");
while (true)
{
c. setCursor (5, 27);
choice = c. getChar ();
if (choice == '1' || choice == '2')
break;
JOptionPane. showMessageDialog (null, "Please enter 1 or 2", "Error", JOptionPane. ERROR_MESSAGE);
}
}
public void askData ()
{
title ();
c. println ("Press 1 for easy difficulty.");
c. println ("Press 2 for medium difficulty.");
c. println ("Press 3 for hard difficulty.");
c. print ("What would you like to do?");
while (true)
{
c. setCursor (6, 27);
difficulty = c. getChar ();
if (difficulty == '1' || difficulty == '2' || difficulty == '3')
break;
JOptionPane. showMessageDialog (null, "Please enter 1, 2 or 3.", "Error", JOptionPane. ERROR_MESSAGE);
}
}
public void display ()
{
int[] scores = new int [10];
title ();
c. println ("Name Difficulty Score");
try
{
BufferedReader input = new BufferedReader (new FileReader ("HighScore.txt"));
for (int x = 0 ; x < 10 ; x++ )
{
String temp = input. readLine ();
c. println (temp );
}
}
catch (IOException e )
{
System. out. println (e );
}
pauseProgram ("continue");
}
public void goodBye ()
{
title ();
c. println ("Made by, Bruno Maillette 2012.");
pauseProgram ("exit");
c. close ();
}
private void pauseProgram (String next )
{
c. print ("Please press any key to " + next + ".");
c. getChar ();
}
private void title ()
{
c. setTextColor (Color. white);
c. setTextBackgroundColor (Color. black);
c. clear ();
c. print (' ', 33);
c. println ("Falling Letters");
c. println ();
}
public static void main (String[] args )
{
FallingLetters p = new FallingLetters ();
p. intro ();
while (true)
{
p. mainMenu ();
if (p. choice == '2')
break;
p. askData ();
p. pack ();
p. setVisible (true);
p. game ();
p. display ();
}
p. goodBye ();
}
}
|
Java: |
import hsa.Console;
import java.util.*;
import java.awt.*;
public class Letter extends Thread
{
Console c;
char letter;
boolean got = false;
Random r = new Random ();
int x = 0, time, time2;
Font font = new Font ("Verdana", 0, 14);
public Letter (Console con, char random, int millis, int nanos )
{
c = con;
letter = random;
time = millis;
time2 = nanos;
}
public void start ()
{
c. setFont (font );
int pos = r. nextInt (620) + 10;
for (x = 40 ; x < 500 ; x++ )
{
c. setColor (Color. black);
c. drawString ("" + letter, pos, x - 1);
c. setColor (Color. white);
c. drawString ("" + letter, pos, x );
if (got )
break;
try
{
super. sleep (time, time2 );
}
catch (InterruptedException e )
{
}
}
c. setColor (Color. black);
c. drawString ("" + letter, pos, x - 1);
}
public void letterGot ()
{
got = true;
}
}
|
Description: |
|
Filesize: |
22.04 KB |
Viewed: |
108 Time(s) |
![Console.JPG](uploads/attachments/thumbs/t_console_186.jpg)
|
Description: |
|
Filesize: |
41.4 KB |
Viewed: |
100 Time(s) |
![Console.bmp](uploads/attachments/thumbs/t_console_479.bmp)
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sun Jan 15, 2012 10:01 pm Post subject: RE:random nextInt not working properly |
|
|
Since the difference is between Java 1.4.2 (used in RtP) and 1.5+ (used by your friend with Dr Java), I'm going to guess that the no-arg constructor for Random (which is what you're using) changed subtly between those versions. In fact, it changed from:
code: | public Random() { this(System.currentTimeMillis()); } |
to:
code: | public Random() { this(++seedUniquifier + System.nanoTime()); }
private static volatile long seedUniquifier = 8682522807148012L; |
Try using the setSeed() method to give each Random instance different seed values.
|
|
|
|
|
![](images/spacer.gif) |
goroyoshi
![](http://compsci.ca/v3/uploads/user_avatars/13875868514dae1bcc8da17.png)
|
Posted: Sun Jan 15, 2012 10:24 pm Post subject: RE:random nextInt not working properly |
|
|
how do i use the setseed method, im trying to use it as r.setSeed (26); but it says invalid constructors
|
|
|
|
|
![](images/spacer.gif) |
ihsh
|
Posted: Thu Feb 02, 2012 6:01 pm Post subject: Re: random nextInt not working properly |
|
|
The problem arises from the fact that you are declaring the variable r as a class variable within the Letter class. This causes trouble because things tend to go wrong when several threads are sharing one common variable (if you ever did an animation project involving threads, you'd know that the colours would wrong if you declare the colour variables as class variables, and are trying to run several animations at the same time). I think it's something about synchronization, but I don't really remember that much about the topic.
So you can eliminate the problem by doing either of the following to the Letter class:
- Declare the variable r in your start() method
- Eliminate the Random class altogether and use (int)(Math.nextRandom()*620)
|
|
|
|
|
![](images/spacer.gif) |
Velocity
![](http://compsci.ca/v3/uploads/user_avatars/1809397984eb9e2888e99b.jpg)
|
Posted: Fri Feb 10, 2012 10:37 am Post subject: RE:random nextInt not working properly |
|
|
what range of numbers are you looking to get?
|
|
|
|
|
![](images/spacer.gif) |
|
|