Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-08-2007, 09:36 AM
Member
 
Join Date: Dec 2007
Posts: 6
www.kwalski.com is on a distinguished road
Random Integers
First off, I am a noob when it comes to programming java. I am a high school student, and have only taken a semester course on java. I am currently working on making a game with simple integer/picture tricks, but I need to know how to create a code that can randomize numbers in between set parameters. IE: A number in between 0 - 20.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-08-2007, 10:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.Random; public class Test { static Random seed = new Random(); public static void main(String[] args) { test(0, 20); test(5, 10); } private static void test(int low, int high) { int min = Integer.MAX_VALUE; int max = -Integer.MAX_VALUE; int range = high - low +1; for(int j = 0; j < 500; j++) { int n = low + seed.nextInt(range); if(n < min) min = n; if(n > max) max = n; } System.out.printf("min = %d max = %d%n", min, max); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-08-2007, 11:04 AM
Member
 
Join Date: Dec 2007
Posts: 6
www.kwalski.com is on a distinguished road
could you add some text explaining each part of that code please...I don't really understand about half of that (sorry, I am kind of garbage at java)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-08-2007, 07:10 PM
Member
 
Join Date: Dec 2007
Posts: 6
www.kwalski.com is on a distinguished road
or if anyone could help out....
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-08-2007, 08:29 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Its an idea of how to explore a question.
Code:
private static void test(int low, int high) { // Find the lowest and highest random values. // Save the extreme values in variables to print later. // Initialize the minimum variable to the highest // possible value for its type (data type int) int min = Integer.MAX_VALUE; // Initialize the maximum value to the lowest // possible value for its data type. int max = -Integer.MAX_VALUE; // Compute random values in the range [low <= value <= high]. // This is the part you play with to get the results you want. int range = high - low +1; // Do enough runs to get definite accurate min and max: for(int j = 0; j < 500; j++) { int n = low + seed.nextInt(range); // If the value "n" is lower than the // current/last-saved value of "min" // save the value of "n" in "min". if(n < min) min = n; // Update "max" with the highest value we find. if(n > max) max = n; } // Print out the results. System.out.printf("min = %d max = %d%n", min, max); }
All of this was a journey to be able to write this:
Code:
int n = low + seed.nextInt(high - low +1);
with confidence.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-09-2007, 09:11 AM
Member
 
Join Date: Dec 2007
Posts: 6
www.kwalski.com is on a distinguished road
that is still kind of confusing, idk if you want to waste any more time trying to explain it to me, but if you could that would be way nice (like which stuff I will need to modify to my own applet and stuff)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-09-2007, 09:11 AM
Member
 
Join Date: Dec 2007
Posts: 6
www.kwalski.com is on a distinguished road
for example the integers and values, which ones do I change and which ones are supposed to be left untouched
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-09-2007, 06:54 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The best way to figure out is to play with the code given in #2 above. When someone posts code that compiles and runs you can run it, make changes, experiment and learn how it works. Learning comes with tinkering and experimenting.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-09-2007, 07:49 PM
Member
 
Join Date: Dec 2007
Posts: 6
www.kwalski.com is on a distinguished road
kk, thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Median of X Integers Hasan New To Java 3 08-12-2008 04:06 PM
[SOLVED] Integers (averages and remainders)...need help Zebra New To Java 4 04-16-2008 03:26 PM
Reading Integers from a text file tress New To Java 3 04-03-2008 05:48 AM
random numbers without random class` carlos123 New To Java 4 01-18-2008 12:44 AM
Convert roman numerals to integers Felissa Advanced Java 2 07-02-2007 01:27 AM


All times are GMT +3. The time now is 08:21 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org