Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to get a random number between 10 and 20  (Read 2187 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2009-01-22 23:35:49 »

Alright i am currently making a text based RPG and im having trouble making a random number between like lets say 10 and 20.

I currently know how to make random numbers between 0 and a number.

this is the code i use to get a random number between 0 and 20.

roll = (int)(Math.random() * 20);

what i need is a number between 10 and 20 any help on how to do this would be nice.


Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Ranger
« Reply #1 - Posted 2009-01-22 23:42:46 »

I like easy questions   Smiley
Random r = new Random();
roll = r.nextInt() % 10 + 10;
Offline Shane75776
« Reply #2 - Posted 2009-01-22 23:55:19 »

so that would make it between 10 and 20 right..

so if i wanted to make it between 20 and 50 would i just do this?
roll = r.nextInt() % 20 + 30;

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Shane75776
« Reply #3 - Posted 2009-01-23 00:18:34 »

i tried what you did and it worked for a number between 10 and 20 but then when i tried getting between 20 and 25 i did r.nextInt() % 20 + 5; and i got -8 lol....

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #4 - Posted 2009-01-23 00:22:32 »

1  
2  
3  
4  
5  
Random rand = new Random()

int number = rand.nextInt(10) + 10;  // number from 10 to 19 inclusive
int number = rand.nextInt(11) + 10;  // number from 10 to 20 inclusive
int number = rand.nextInt(6) + 20;  // number from 20 to 25 inclusive


The number inside the nextInt is the range of numbers that you want.  The number added after is the bottom of that range.

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #5 - Posted 2009-01-23 00:25:52 »

[...]
this is the code i use to get a random number between 0 and 20.

roll = (int)(Math.random() * 20);
[...]

That gives you 0 to 19 (inclusive).

random.nextInt(20)+1

Gives you 1 to 20 (inclusive). Like a typical 20 sided dice would do.

random.nextInt(biggestPossibleValue+1)+smallestPossibleValue

Gives you smallestPossibleValue to biggestPossibleValue (inclusive, of course).

弾幕 ☆ @mahonnaiseblog
Offline Shane75776
« Reply #6 - Posted 2009-01-23 00:29:54 »

Thank you for the help. i have 1 other questionthough and i might sound stupid but how do you get all the class files and pictures for a JFrame into an executable jar file?

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline steveyO
« Reply #7 - Posted 2009-01-23 01:23:42 »

If you are using Eclipse then easiest way is to Right-Click on your project (in Package Explorer).. Then 'Export'    --> Java 'JAR file'  etc..
After which you can select with pictures/classes you want to include.

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Offline Shane75776
« Reply #8 - Posted 2009-01-23 01:51:02 »

thank you i will try that

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Renoria

Junior Member




...


« Reply #9 - Posted 2009-01-23 07:02:50 »

public int rand(int s, int e) {
 return (s + (int) (Math.random() * (e-s)));
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2009-01-23 07:27:36 »

random.nextInt(biggestPossibleValue+1)+smallestPossibleValue

Gives you smallestPossibleValue to biggestPossibleValue (inclusive, of course).

Not quite.


random.nextInt((biggestPossibleValue-smallestPossibleValue)+1)+smallestPossibleValue

Gives you smallestPossibleValue to biggestPossibleValue (inclusive, of course).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #11 - Posted 2009-01-23 18:39:33 »

Heh. That happened because I was unable to find a nice name for deltaThing... and then I thought... hey, using the biggest possible value would be nicer... but I didn't pay enough attention. Wink

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!