Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a random double value between -1 and 1

Tags:

java

random

I'm creating a Neural Network in Java and need to create a method to generate random weights initially.

I need to create a function that returns a random double value between -1 and 1 but not sure of the logic for doing this so any help would be most appreciated.

like image 978
unleashed Avatar asked Feb 21 '12 21:02

unleashed


1 Answers

You can use the Random class's nextDouble() method.

Random rng = new Random();
// to get a double between -1 and 1
return rng.nextDouble() * 2 - 1; // rng.nextDouble() is between 0 and 1
like image 134
Louis Wasserman Avatar answered Oct 18 '22 02:10

Louis Wasserman