Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random

 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have little(?) question for You,

so I would like to create Random manager.

In Thinking in Java 4 (Book) I saw little example with Random.java class.

So, I would like to create something like "Random Reward" for my game.

Let me explain it, I have item handler for my item in my game, and if player clicked on it it should give him random money.

I just need help with random.

It should be with interval (from -1000 to 1000) So player has chance to win or loose.

But as I can see Random works only with positive numbers (+) so i would like to know how can i fix it.

Also would be great if someone would show me little example with:

if(drawn number is negative)

//something

Negative - I mean (-) for example - 234

Thans in advice.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Mintalm wrote:It should be with interval (from -1000 to 1000) So player has chance to win or loose.

But as I can see Random works only with positive numbers (+) so i would like to know how can i fix it.

That's very simple. Generate a random number between 0 and 2000 and subtract 1000 - then you'll have a number in the range -1000 to 1000.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
get a random number from 0 to 2000...

then subtract 1000 from it.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, can you show me just a little example?

I would like to understand it better.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Range from -10 to 10

Random gen = new Random();

int rand = (gen.nextInt(20) - 10);


-Hunter
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that is quite right... from the API:

int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.


so your example will give values from 0 to 19, shifted to -10 to 9.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i saw it too, so is there any one who can show me valid example, please?

It would help me a loot.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew:

If you want to go from -10 to 10, you need to use 21 (you have to count the 0). It should be easy to extrapolate that to any number you need.

John.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Mintalm wrote: is there any one who can show me valid example, please?

???

The example given WAS valid. It is real, working code. It give a range from -10 to 9.

We really try NOT to give out answers here. We feel the best way to learn is to help you figure it out for yourself, by guiding you there. To be honest, I almost deleted Hunter's post, since it all but gives the answer away.

Try do write the code yourself, based on what Hunter provided and the subsequent corrections. Post what you think is right, and we'll guide you towards the correct answer.

But please don't expect anybody to just hand you the complete solution.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Mathew Mintalm wrote: is there any one who can show me valid example, please?

???

The example given WAS valid. It is real, working code. It give a range from -10 to 9.

We really try NOT to give out answers here. We feel the best way to learn is to help you figure it out for yourself, by guiding you there. To be honest, I almost deleted Hunter's post, since it all but gives the answer away.

Try do write the code yourself, based on what Hunter provided and the subsequent corrections. Post what you think is right, and we'll guide you towards the correct answer.

But please don't expect anybody to just hand you the complete solution.


Yes, You're right. Anyway thanks, it works.

My last question is now, how can I check if my this random number was negative (-)?

Maybe you can post few hints.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh, I was stupid..

ok it should works now for me.

if(rand <0)
//is negative..

Ok guys, thank you all for help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic