PDA

View Full Version : random function in java ??



abhilashm86
June 12th, 2009, 12:37 PM
well if i use random numbers between an interval like in c++, javac is throwing error, this is what is did,


public class j4{
public static void main(String args[]){
double sum=0;
double randomize(0,100);
double x=Math.random();
sum+=x;
System.out.println("value is "+sum);
}
}


so when i tried, javac said needs to be double, so changed,
javac j4.java
j4.java:4: ';' expected
double randomize(0,100);
^
1 error

pokerbirch
June 12th, 2009, 12:46 PM
I'm not a Java programmer, but:

Is randomize() a function? If it's a function, shouldn't you be assigning it to a variable?

gvoima
June 12th, 2009, 12:49 PM
AFAIK there's no such method in the java Random class. Or any other like it.

pokerbirch
June 12th, 2009, 12:53 PM
Perhaps he's thinking along the lines of VB6 which has a Randomize() function which creates a random seed. It's not required in Java. A quick Google found this:

http://www.javapractices.com/topic/TopicAction.do?Id=62

rocketflame
June 12th, 2009, 12:55 PM
Math.random()

gives you a random double between 0 and 1
i dont know c++, but i'm guessing you want it between 0 and 100?
if thats the case you can always use


(Math.random() * 100)

then try getting rid of

double randomize(0,100);

and see if it works.

abhilashm86
June 12th, 2009, 01:15 PM
(Math.random() * 100)

.

yes good, it worked........

abhilashm86
June 12th, 2009, 01:17 PM
I'm not a Java programmer, but:

Is randomize() a function? If it's a function, shouldn't you be assigning it to a variable?

yes true randomize is user defined function, we use srand(time(0)) and then initialize int x=rand()%100; to get interval,