Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 06-21-2008, 10:52 PM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
How do I generate random numbers in a certain range using the random class?
I know how to use Math.random for this, but how would I generate random numbers using the random class?

Say I want a number between 40 and 50, inclusive--how would I do this?

What i have in mind is:

int randomNumber = random.nextInt(max) + min;

where max is 50 and min is 40. Is this correct?

Thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-21-2008, 11:51 PM
Senior Member
 
Join Date: Jun 2008
Posts: 220
Fubarable is on a distinguished road
Random numbers always go from 0 to the max. And you know that you want to start with 40 and add a random number to it. So how do you do this? Think on this and you will figure this out.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-21-2008, 11:57 PM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
I have searched it for the past 30 min and did not find an exact method for int. Can anyone help me out here? I need this before proceeding with my program.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-22-2008, 04:21 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 526
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
use else/if statement to filter the undesired generated integer number....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-22-2008, 04:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
import java.util.Random; public class Test { public static void main(String[] args) { Random seed = new Random(); // range [40 - 50] int base = 40; int variable = 10; int min = Integer.MAX_VALUE; int max = -Integer.MAX_VALUE; for(int i = 0; i < 100; i++) { // 0 <= range returned < specified value int next = 40 + seed.nextInt(variable + 1); if(next < min) min = next; if(next > max) max = next; } System.out.printf("min = %d max = %d%n", min, max); } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-22-2008, 04:31 AM
Senior Member
 
Join Date: Jun 2008
Posts: 220
Fubarable is on a distinguished road
Quote:
Originally Posted by frasifrasi View Post
I have searched it for the past 30 min and did not find an exact method for int. Can anyone help me out here? I need this before proceeding with my program.
You don't need an "exact method". We've given you enough hints, now all you need is a functioning brain.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-22-2008, 04:58 AM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
Ok, thanks. I searched extensively and didn't come across anything.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-22-2008, 06:07 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
rand(max - min) + min.
__________________
My IP address is 127.0.0.1
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
Random numbers jithan Advanced Java 3 06-14-2008 03:04 PM
random numbers without random class` carlos123 New To Java 4 01-17-2008 11:44 PM
random numbers carlos123 New To Java 1 12-22-2007 03:56 AM
Generate a random number romina New To Java 1 08-07-2007 06:23 AM
How to generate random number in java fernando New To Java 1 08-01-2007 08:32 PM


All times are GMT +3. The time now is 01:05 AM.


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