curious random number problem


M

Michael Powe

Hello,

I was following along in a book about JSP and made up one of the
examples in NetBeans. I didn't like the way the author set up the
example, as a scriptlet (I don't like the code on the page), so I
modded it slightly, creating a "code behind" class and importing it
into the page. Nothing elaborate here, I'm just playing around.

This is supposed to simulate flipping a coin and updating the coin
count "heads/tails" on a JSP. There's something wrong with the way
I'm doing it, though, which causes the coin to periodically "land on
its edge," so to speak, so I had to put in some code to prevent a
NullPointerException from blowing up the page.

But what is wrong with this code? What am I doing that generates a
NPE if i don't include the "none" catcher?

Thanks.

mp

package wdjsp;

public class RandomNumbers {

public static int randomNumber = 0;

public static int getRandomNumber(){
double rand = generateRandomDouble();
randomNumber = generateRandomInt(rand);
return randomNumber;
}
private static double generateRandomDouble(){
return Math.random();
}
private static int generateRandomInt(double number){
return (int)Math.round(number + 1);
}
} // end class

8<=================================================>8

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="wdjsp.RandomNumbers"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1>Random Number Test</h1>

<%
String winner = null;
int answer = 0;

synchronized (session){
if (session.isNew()){
session.setAttribute("heads",new Integer(0));
session.setAttribute("tails",new Integer(0));
session.setAttribute("none", new Integer(0));
}
}

if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
winner = "heads";
} else if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 2){
winner = "tails";
} else {
winner = "none";
}

if (!winner.equals("none")){
int oldVal = ((Integer)session.getAttribute(winner)).intValue();
session.setAttribute(winner, new Integer(oldVal + 1));
}

%>

<table border="1" width="50%" cellpadding="1">
<thead>
<tr>
<th>heads</th>
<th>tails</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= session.getAttribute("heads")%></td>
<td><%= session.getAttribute("tails")%></td>
</tr>
</tbody>
</table>
<%

if (winner == "none"){
int noneCount = ((Integer)session.getAttribute("none")).intValue();
session.setAttribute("none", new Integer(noneCount+1));
out.println("None == " + (session.getAttribute("none")) + "<br />\n");
out.println("answer == " + answer);
}

%>


</body>
</html>
 
Ad

Advertisements

D

Dale King

Michael said:
This is supposed to simulate flipping a coin and updating the coin
count "heads/tails" on a JSP. There's something wrong with the way
I'm doing it, though, which causes the coin to periodically "land on
its edge," so to speak, so I had to put in some code to prevent a
NullPointerException from blowing up the page.

But what is wrong with this code? What am I doing that generates a
NPE if i don't include the "none" catcher?

package wdjsp;

public class RandomNumbers {

public static int randomNumber = 0;

public static int getRandomNumber(){
double rand = generateRandomDouble();
randomNumber = generateRandomInt(rand);
return randomNumber;
}
private static double generateRandomDouble(){
return Math.random();
}
private static int generateRandomInt(double number){
return (int)Math.round(number + 1);
}
}

I didn't look into it for the NPE but you are going about it all wrong
to generate a random coin toss. Use the java.util.Random class which has
a nextBoolean method that will work for generating random true or false
values.
 
D

Dale King

Michael said:
This is supposed to simulate flipping a coin and updating the coin
count "heads/tails" on a JSP. There's something wrong with the way
I'm doing it, though, which causes the coin to periodically "land on
its edge," so to speak, so I had to put in some code to prevent a
NullPointerException from blowing up the page.
if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
winner = "heads";
} else if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 2){
winner = "tails";
} else {
winner = "none";
}

In this if you are essentially throwing the coin twice (you call
getRandomNumber twice). You are only declaring it tails if you get 2
tails in a row. If you get tails followed by heads you say there was no
winner.

It would be a lot cleaner if you pulled the assignment to answer out of
the if statements.
 
R

Red Orchid

Message-ID: said:
if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
winner = "heads";
} else if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 2){
winner = "tails";
} else {
winner = "none";
}


I think that the above code maybe be wrong.

Because ...
The above code calls 'getRandomNumber' twice for one flip.
I think that the following code is valid.

<code>

answer = .... // get random (1 or 2)

switch (answer) {
case 1:
.. // do something
break;

case 2:
.. // do something
break;

default:
assert false : "Impossible answer";
break;
}


Or

boolean answer;

....

answer = .... // get random (true or false) using Random.nextBoolean

</code>
 
M

Michael Powe

Red> I think that the above code maybe be wrong.

Red> Because ... The above code calls 'getRandomNumber' twice for
Red> one flip. I think that the following code is valid.


Yes, thanks to you and Dale for pointing this out. Definitely a
blunder. I'll fix it.

Then I'll try again without the "none" category, which is how I was
protecting against the NPE.

Thanks.

mp
 
M

Michael Powe

"Dale" == Dale King <"DaleWKing [at]gmail [dot] com"> writes:


Dale> I didn't look into it for the NPE but you are going about it
Dale> all wrong to generate a random coin toss. Use the
Dale> java.util.Random class which has a nextBoolean method that
Dale> will work for generating random true or false values.

Thanks, I'll look at that. I was really just copying what was in the
book and modifying it according to my own preferences.

I wish I knew what was causing the error. It isn't a big deal of
course, it's just a trivial JSP example. It's just annoying to not
understand where the error takes place.

Thanks.

mp

--
Michael Powe (e-mail address removed) Naugatuck CT USA
"When a person behaves in keeping with his conscience, when he
tries to speak as a citizen even under conditions where
citizenship is degraded, it may not lead to anything, yet it might.
But what surely will not lead to anything is when a person calculates
whether it will lead to something or not." -- Vaclav Havel, 1989
 
Ad

Advertisements

M

Mike Schilling

Michael Powe said:
Hello,

I was following along in a book about JSP and made up one of the
examples in NetBeans. I didn't like the way the author set up the
example, as a scriptlet (I don't like the code on the page), so I
modded it slightly, creating a "code behind" class and importing it
into the page. Nothing elaborate here, I'm just playing around.

This is supposed to simulate flipping a coin and updating the coin
count "heads/tails" on a JSP. There's something wrong with the way
I'm doing it, though, which causes the coin to periodically "land on
its edge," so to speak, so I had to put in some code to prevent a
NullPointerException from blowing up the page.

But what is wrong with this code? What am I doing that generates a
NPE if i don't include the "none" catcher?

The fact that winner is originally set to null, and without the "none"
catcher, there's no guarantee it gets set to anything else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top