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

generate random no. in a range?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
I need to generate random numbers from an interval range of [30-50]. I've searched and I find how to generate random numbers between a range but I dont know how to generate number between an interval range?

thnx
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
An object of class Random (from package java.util) can do just that.



Random method nextInt comes in two forms:
nextInt(), with no arguments, returns an integer in the range -2,147,483,648 to + 2,147,483,647.
nextInt( scale ) returns an integer from 0 to scale - 1. scale is the number of integers in the range.

To get a number from within a range that does not begin with 0 you must use a shifting value:
shift + nextInt( scale )

To get the scale for a range you need the total number of integers in that range. For example, the range
5 to 10 includes 6 integers ( 10 - 5 + 1 ).
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear thank you
but I did this before and it gives me integer number while i need it in an ( Interval ) range. I also try this one


which gives me double(interval) but I dont know how to identify a range for it? for example between [20-30]? can you help me out?

thank you,
sahar.
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41


nextDouble() returns a number between 0 (inclusive) and 1 (exclusive). This multiplied by ( end - start ) will give a number
between 0 and almost ( end - start ). Add that to start and the result is between start (inclusive) and end (exclusive).

By the way, I don't really know this for sure. I just looked it up in the API. I tried it in a for loop though, and it never gave me a number greater than or equal to end, nor did it give me a number less than start.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Your question is by no means clear, Sahar Sa. Please explain exactly what you need.
Your explanation is correct, Greg Stevens.
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
hi,
Dear ritchie: sorry, let me explain again: I just need to generate a set of Double numbers between a range of [low,high] e.g. [30-50]. I know that its too simple but I can not run it .
Dear steven :
1)I've tried to use this code: randomNumbers.nextDouble() as you said, but it gives me: non-static method cannot be referenced from a static context error. so I checked in internet an find an example said I have to make a constructor then instantiate it from main so I remove the non-static method eith referenced from static content. (If I've understand well!?) so I didi it as follow:



2) still got error: S() needs returned type!!! so I defined void then
3) still got 2 error: (1) class s (instantiate in main method ) can not be find (2) can not find class randomNumbers() .

I really dont know how to fix? that would be your kind if you show me an executable example of this!

sorry for long boring post.
thank you sooo much!
Sahar.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

It seems that you're really confusing things.

Why do you make an instance of Random and call it 'max'? Why do you declare two variables 'low' and 'high' and then you don't use them for anything? Please make sure that you understand exactly what your own code does.

This is a simple problem, and the answer has already been given.

The method nextDouble() of class Random gives you random numbers between 0 and 1. If you want the number to be between 30 and 50 instead of 0 and 1, you simply multiply the result by the length of the interval (50 - 30 = 20) and add the start of the interval to it (30):



You could make a method out of it (this version uses Math.random(), which also returns a random double value between 0 and 1):


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
More details of the nextDouble method here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generate random no. in a range?