aspose file tools*
The moose likes Beginning Java and the fly likes Random Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random" Watch "Random" New topic
Author

Random

Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

get a random number from 0 to 2000...

then subtract 1000 from it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Please, can you show me just a little example?

I would like to understand it better.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Range from -10 to 10

Random gen = new Random();

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


-Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Feb 21, 2010
Posts: 103

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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Feb 21, 2010
Posts: 103

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

Joined: Feb 21, 2010
Posts: 103

Ohh, I was stupid..

ok it should works now for me.

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

Ok guys, thank you all for help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random