• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mathrandom every x  RSS feed

 
Gustav Andersson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I need some help. I am trying to make a object change x coordinate everytime it falls down to a y coordinate and then it should start over on the top but with a new x coordinate.
For this im using math.random(). But I want it to randomly be picked with x = 17.
At the moment im using = x = (int)(Math.random()*408) and 408 is end of the frame.
So to summarize it, like this (x can only be 17, 34, 51 etc):

x = 0
x = 17
x = 34
x = 51

And this want I to be in random order, like this for example:

x = 17
x = 0
x = 51
x = 34

It was pretty hard to explain, but how you understand.
Sincerely,
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need a random multiple of 17 then get a random integer in a correct range and multiply it by 17. Simple.
 
Gustav Andersson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:If you need a random multiple of 17 then get a random integer in a correct range and multiply it by 17. Simple.


I don't understand, can you show how you mean?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said you want a random integer like 0, 17, 34, 51...
So, get a random integer like 0, 1 , 2, 3...
Multiply the value by 17 and you'll get 0, 17, 34, 51...
 
Gustav Andersson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, I have already multiplied with the highest number I want the x to be which is 408. And thats 17, 24 times.
 
Gustav Andersson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved it, thanks!
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using Math#random for arithmetic. Create an instance of Random and use one of its methods. You will find more discussion here. As you have already been told, then simply multiply the result by 17.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!