Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i generate random number in some range(for example 1-100) in java?
Posted

1 solution

 
Share this answer
 
Comments
Coder93 25-Jan-13 5:36am    
i use your solution , but it always generate same random number, what should i do ?
OriginalGriff 25-Jan-13 5:49am    
Move the definition of randomGenerator outside the method - otherwise it is preset each time you call it.
If you look at the definition of Random it is preset to the system clock, so if you call the method 10 times in the same millisecond and create a new Random each time, you will get the same numbers.
Coder93 25-Jan-13 5:53am    
I run the code after 20 min but, it generate the random number like before!!!
OriginalGriff 25-Jan-13 5:55am    
Copy and paste the exact code you used.
Coder93 27-Jan-13 14:05pm    
It is part of my code :

public class myclass {
private ……;
private Random random;
…….
public copkmeans (double [][] dataSet , int [][] constraint , int k, int i, int j , int l) throws IOException {
…….
random = new Random(i);
}
private void makeCluster (){
int randomNum;
int range = k;
while(range > 0){
randomNum = random.nextInt(i)+1;
System.out.println("random"+randomNum);
for(int m = 1; m < l ;m++){
if(constraint[m][1] == randomNum )
if(constraint[m][3] == 1)
if(clusters.contains(constraint[m][2])){
break;
}
if(constraint[m][2] == randomNum )
if(constraint[m][3] == 1)
if(clusters.contains(constraint[m][1])){
break;
}
clusters.add(randomNum);
used.add(randomNum);
myItem temp = new myItem(m, dataset , j);
temp.setHole();
clusterCenter.add(temp);
range--;
break;
}
}
int v = 1;
for (int a = 0 ; a < clusters.size() ; a++) {
partition[clusters.get(a)] = v;
v++;

}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900