This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Random Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Random" Watch "Random" New topic
Author

Random

Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 102

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
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

get a random number from 0 to 2000...

then subtract 1000 from it.


Never ascribe to malice that which can be adequately explained by stupidity.
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 102

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: 490

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: 9939
    
    6

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: 102

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: 9939
    
    6

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: 102

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: 102

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Random
 
Similar Threads
Coding Approach to a Situation
Calling a variable form the main class
why can't some people play my game
Text highlighting
OO design question?