-1

i wrote a simple html file that prints 10 random numbers using javascript. javascript object in turn uses java.util.Random class (live connect) to output the result! but i am not getting exact output when using with functions and events like onLoad or onclick!

<html>
<body onLoad="hello()">
<script language="javascript">
function hello()
{
var i=0;
for(;i<10;i++)
{
var j=new java.util.Random(i);
document.writeln(j);
}
}
</script></body></html>

And i can get output if we dont use function!

<html>
<body >
<script language="javascript">
var i=0;
for(;i<10;i++)
{
var j=new java.util.Random(i);
document.writeln(j);
}
</script></body></html>

can anybody help me with this bug!

2 Answers2

3

Use this:

var j=Math.random()*i; instead of var j=new java.util.Random(i);

Naveed Ahmad
  • 3,176
  • 1
  • 15
  • 18
  • but my doubt is, the code is working perfectly when we directly write it inside script tag and the code is not working when we put it in a function??!! –  Apr 19 '11 at 08:52
0

Perhaps this is the correct syntax?

for(;i<10;i++){
  Random r =  new Random();
  int j =  r.nextInt(i)
  document.writeln(j);
}
Calum
  • 5,308
  • 1
  • 22
  • 27