The Artima Developer Community
Sponsored Link

Java Answers Forum
Random Number Problem

10 replies on 1 page. Most recent reply: Feb 24, 2003 4:23 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Random Number Problem Posted: Feb 12, 2003 2:19 PM
Reply to this message Reply
Advertisement
For some reason i keep getting the same random number every time the program runs.

How do i fix this?

My code for the random number is a follows:

int random = (int) (Math.random() * 100);

thanks


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Random Number Problem Posted: Feb 12, 2003 4:56 PM
Reply to this message Reply
Well, that is, of course, possible, just not very probable. I would guess that the Math module always seeds the Random object it creates with the time, so maybe your system clock is in limbo. Chances are it is a bug in your code. Let's see the rest of it.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Random Number Problem Posted: Feb 12, 2003 4:57 PM
Reply to this message Reply
Ye gads! I just read that first sentence. I think there are more commas than words. How did I ever make it though my college English course? I'm ashamed of myself. I will refrain from using commas for the rest of the day.

AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Re: Random Number Problem Posted: Feb 12, 2003 5:01 PM
Reply to this message Reply
heres the whole program as of now:

import javabook.*;
import java.awt.*;
import java.lang.Math;

public class SecretNumber
{
public static void main (String[] args)
{
int count = 0;
int guess = 0;
int random = (int) (Math.random() * 100);
int trysLeft = 0;
int totalTrys = 6;

do
{
MainWindow mainWindow;
mainWindow = new MainWindow("Welcome To Take A Guess");

InputBox inputBox;
inputBox = new InputBox(mainWindow, "Guess A Number");

guess = inputBox.getInteger("Please Pick A Number Between 0 and 100: ");

if(guess < random)
{
System.out.println("Your Guess Was Too Low.");
count++;
trysLeft = totalTrys - count;
System.out.println("You have " + trysLeft + " guesses left.");
}

if(guess > random)
{
System.out.println("Your Guess Was Too High.");
count++;
trysLeft = totalTrys - count;
System.out.println("You have " + trysLeft + " guesses left.");
}

if(guess == random)
System.out.println("Good Job! You Guessed The Number");

}while(guess != random && trysLeft != 0);


}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Random Number Problem Posted: Feb 12, 2003 8:08 PM
Reply to this message Reply
Doesn't look like it would get a different number every time it runs. Look at it with a debugger or put a
System.out.println( "Hint: " + Integer.toString(random));
right after
int random = (int) (Math.random() * 100);
and you'll see it is usually different.

A few style notes:
- I don't know what the javabook and stuff is, but it looks like GUI stuff; mixing GUI and console output is a bit confusing, but I guess you are just trying to get things working at this point and that is not the final plan.
- I wouldn't import the Math module which has a random method have a variable called random -- that is a bit confusing.
- I would make a special method for generating the magic number to guess. Then it is easy to unit test; so I would then create a test Suite for it, with the Artima SuiteRunner, or JUnit.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Random Number Problem Posted: Feb 13, 2003 10:58 AM
Reply to this message Reply
Oops, that first sentence should be "It doesn't look like it will get the same number every time it runs."

sunilgiri

Posts: 7
Nickname: sunilgiri
Registered: Feb, 2003

Re: Random Number Problem Posted: Feb 22, 2003 11:55 AM
Reply to this message Reply
That,s ok

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Random Number Problem Posted: Feb 22, 2003 5:56 PM
Reply to this message Reply
When I run your random number generator code on my pc in the following, I do not see the same random number every time the program runs. It sure looks random enough to me.
I don't think your problem is in your java code.


public class RandomNumberTest{
public static void main (String[] args){
for (int i = 0; i < 20;i++){
int random = (int) (Math.random() * 100);
System.out.print(random + " ");
}
}
}

deliquentMind

Posts: 3
Nickname: deliquent
Registered: Feb, 2003

Re: Random Number Problem Posted: Feb 22, 2003 9:18 PM
Reply to this message Reply
try this method:

public double dblRandom(double low, double high) {
return Math.random()*(high - low) + low;}

public int intRandom(int low, int high) {
int nb = high - low + 1;
return (int)(Math.random()*nb + low);
}

[/prep]

to use:

int number = intRandom(1,100);

Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Random Number Problem Posted: Feb 23, 2003 7:32 AM
Reply to this message Reply
//try this

java.util.Random rd = new java.util.Random(System.currentTimeMillis());
int rndNumber = rd.nextInt(100) + 1;

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Random Number Problem Posted: Feb 24, 2003 4:23 PM
Reply to this message Reply
It's not necessary to seed the random number gererator; if you look at the documentation you'll see that it is done by default.

Flat View: This topic has 10 replies on 1 page
Topic: ORA-01461 Previous Topic   Next Topic Topic: reading in a csv  file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use