
-----------------------------------
goroyoshi
Sun Jan 15, 2012 8:32 pm

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

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


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;
    }
}


-----------------------------------
DemonWasp
Sun Jan 15, 2012 10:01 pm

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()); }[/code]

to:
[code]public Random() { this(++seedUniquifier + System.nanoTime()); }
private static volatile long seedUniquifier = 8682522807148012L;[/code]

Try using the setSeed() method to give each Random instance different seed values.

-----------------------------------
goroyoshi
Sun Jan 15, 2012 10:24 pm

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

-----------------------------------
ihsh
Thu Feb 02, 2012 6:01 pm

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. :roll: 

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)

-----------------------------------
Velocity
Fri Feb 10, 2012 10:37 am

RE:random nextInt not working properly
-----------------------------------
what range of numbers are you looking to get?
