java - I have an error with min and max nextInt -




this question has answer here:

i want make speed on vector x random min -5 , max 5. everytime run game crashing. problem in random because when set velx = 2 or 3 works perfectly, me please. (sorry english, hope understand me)

velx = random.nextint(5 - -5) + -5; 

you want this:

velx = random.nextint(10) - 5; 

(generate random int btw 0 , 10, subtract 5, giving range btw -5 (inclusive) , 5 (exclusive). if want 5 inclusive well, change nextint() parameter 11.


otoh shouldn't use real random, rather bell curve (values in middle appearing more outliers). 1 easy way achieve have list values (once outliers , more middle values) , pick randomly among them. might want play around values see works best you.

// once, during initialization list<integer> values = new arraylist<>(); values.add(-5); values.addall(collections.ncopies(2, -4)); values.addall(collections.ncopies(3, -3)); values.addall(collections.ncopies(5, -2)); values.addall(collections.ncopies(8, -1)); values.addall(collections.ncopies(12, 0)); values.addall(collections.ncopies(8, 1)); values.addall(collections.ncopies(5, 2)); values.addall(collections.ncopies(3, 3)); values.addall(collections.ncopies(2, 4)); values.add(5);  // every time velx = values.get(random.nextint(values.size())); 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -