Can anyone help me on the error messages generated in test1 ? - i'm playing with genetic algorithms | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone help me on the error messages generated in test1 ? - i'm playing with genetic algorithms

26th Apr 2017, 3:28 PM
David
2 Answers
+ 10
random() is not a method. You can import the Random class: import java.util.Random; // top of program Then use either: Random name = new Random(); name.nextInt(((max - min) + 1) + min); or new Random().nextInt(((max - min) + 1) + min); To generate a random number (int) from (x,y); where max = y, min = x. Let me know if there are any more errors after this fix. edit: To add to what I said, for a random number from 0 to x-1 you can just use Math.random(). So for a random number where you wrote: "int midPoint = int(random" etc.. to the array length. You could do: int midPoint = (int)(Math.random()*genes.length); Instead of having to make/use a Random object. extra info: This is allowed because the random() method is static, so you can call the method random() in Math without creating an instance of the Math class.
26th Apr 2017, 4:19 PM
Rrestoring faith
Rrestoring faith - avatar
0
Thanks for your help restoring faith - unfortunately - I get these error messages: ..\Playground\:9: error: illegal start of type import java.util.Random; ^ ..\Playground\:9: error: ';' expected import java.util.Random; ^ ..\Playground\:9: error: illegal start of type import java.util.Random; ^ ..\Playground\:9: error: ';' expected import java.util.Random; ^ ..\Playground\:9: error: <identifier> expected import java.util.Random; ^ ..\Playground\:33: error: '.class' expected int midpoint = int(random(genes.length)); ^ ..\Playground\:33: error: ';' expected int midpoint = int(random(genes.length)); ^ 7 errors This is just from adding: import java.util.Random; // top of program
27th Apr 2017, 8:59 AM
David