javascript and live connect

399 Views Asked by At

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

There are 2 best solutions below

0
Calum On

Perhaps this is the correct syntax?

for(;i<10;i++){
  Random r =  new Random();
  int j =  r.nextInt(i)
  document.writeln(j);
}
1
Naveed Ahmad On

Use this:

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