Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random float between negative and positive opengl es java

Im studying opengles. I want to know how can i generate between -1 to 1. Thats because opengl normalized device coordinates is only between -1 and 1. Some mentioned that random float is only between 0.0 and 0.9999999.

here is my code

 points.addParticles(new GeoPoint(-random.nextFloat(),random.nextFloat(),random.nextFloat()),180);

Thats for x,y,z and random color.

enter image description here

I just want to generate random points inside the screen with random location.

like image 797
gamdevNoobie Avatar asked Dec 03 '13 07:12

gamdevNoobie


2 Answers

Well Random.nextFloat gives a value greater than or equal to 0, and less than 1 - i.e. a range of [0, 1). So to map that to a range of [-1, 1) you just need to multiply by 2 and subtract 1.

float value = random.nextFloat() * 2 - 1;

In general, to get a value in the range [x, y) you would use

random.nextFloat() * (x - y) + x
like image 91
Jon Skeet Avatar answered Nov 20 '22 15:11

Jon Skeet


Well you could use one more random, which would decide if you multiply by 1 or (-1)? if random >0.5 (first_random *1) else (first_random*(-1))

like image 38
futura Avatar answered Nov 20 '22 14:11

futura