if i use java.util.Random i can create random numbers.
But how do i can define a Random object that its methods like
nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
13 61478
if i use java.util.Random i can create random numbers.
But how do i can define a Random object that its methods like
nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
Math.random() will do the job for you there. - int x = 100 + (int)(Math.random()*200);
Ok i got the result with ur solnution. But i wanted to know whether i can set a random object to return value between a specified lower limit and upper limit so that i need not to worry about the complexity of calculations
Ok i got the result with ur solnution. But i wanted to know whether i can set a random object to return value between a specified lower limit and upper limit so that i need not to worry about the complexity of calculations
You would have to write your own custom method for it.
Something like - public int nextInt(Random r, int lower, int higher) {
-
int ran = r.nextInt();
-
double x = (double)ran/Integer.MAX_VALUE * higher;
-
return (int)x + lower;
-
}
-
You would have to write your own custom method for it.
Something like - public int nextInt(Random r, int lower, int higher) {
-
int ran = r.nextInt();
-
double x = (double)ran/Integer.MAX_VALUE * higher;
-
return (int)x + lower;
-
}
-
This will return values from lower to higher + lower so you will need to scale it again
I got the soltion from custom method.
Thanks for giving the better solution.
Ok i got the result with ur solnution. But i wanted to know whether i can set a random object to return value between a specified lower limit and upper limit so that i need not to worry about the complexity of calculations
another alternative -
Random r=new Random();
-
int n=(r.nextInt(101) + 100);
-
r.nextInt(101) returns a value between 0 and 100 and you then add 100
if i use java.util.Random i can create random numbers.
But how do i can define a Random object that its methods like
nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
Hello, friend,
Try the following, plz:
public class Ex {
public static void main(String[] args) {
System.out.println(Math.round(Math.random()*100+10 0));
}
}
Vladimir1
if i use java.util.Random i can create random numbers.
But how do i can define a Random object that its methods like
nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
Here was my simple solution to random numbers in a range
import java.util.Random;
public class Random2 extends Random{
public int nextInt(int lower,int upper){
return nextInt((upper-lower+1))+lower;
}
}
You would have to write your own custom method for it.
Something like - public int nextInt(Random r, int lower, int higher) {
-
int ran = r.nextInt();
-
double x = (double)ran/Integer.MAX_VALUE * higher;
-
return (int)x + lower;
-
}
-
Bzzzt! Thanks for playing; close but no cigar ;-)
Better make that line: -
double x = (double)ran/Integer.MAX_VALUE * (higher-lower);
-
Using "nextInt(higer-lower)+lower" is also an option.
kind regards,
Jos
kind regards,
Jos
Bzzzt! Thanks for playing; close but no cigar ;-)
Better make that line: -
double x = (double)ran/Integer.MAX_VALUE * (higher-lower);
-
Using "nextInt(higer-lower)+lower" is also an option.
kind regards,
Jos
kind regards,
Jos
Did I do that?
Thanks for digging up this thread guys. Otherwise my mistake would not have been noticed.
Did I do that?
Thanks for digging up this thread guys. Otherwise my mistake would not have been noticed.
I didn't dig up anything; I'm innocent as always; fivekiller found it; good work ;-)
kind regards,
Jos
@r035198x
This should be the following: - int x = 100 + (int)(Math.random()*100);
- import java.util.Random;
-
public class Question {
-
-
-
public static void main(String[] args) {
-
Random random = new Random ();
-
int x = 100 + (int)(Math.random()*200);
-
System.out.println(Math.round(Math.random()*200));
-
}
-
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nicholas Geraldi |
last post by:
Im looking for a decent random number generator. Im looking to make a large
number of random numbers (100 or so, if not more) in a short period of time
(as fast as possible).
the function i was...
|
by: Chris Dutrow |
last post by:
I searched around on the net for a bit, couldn't find anything though.
I would like to find some code for a function where I input A Range Of
Integers
For example: Function( 1, 100 );
And the...
|
by: JoelPJustice |
last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet...
|
by: Jim Michaels |
last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's...
|
by: johntilster |
last post by:
How do I get uniform random numbers in the log domain? I need a random
number in the log domain between -infinity and -0.69314718, which is
equivalent to a range from 0 to 0.5.
I usually use...
|
by: chico_yallin |
last post by:
I just wana make a random id number based on4 digits-for examples??
Thanks in Advance
Ch.Yallin
|
by: Peter Oliphant |
last post by:
I would like to be able to create a random number generator that produces
evenly distributed random numbers up to given number.
For example, I would like to pick a random number less than 100000,...
|
by: carlos123 |
last post by:
i need a formula to generate 100 random numbers 1-20
any suggestions?
|
by: bilgekhan |
last post by:
What is the correct method for generating 2 independent random numbers?
They will be compared whether they are equal.
What about this method:
srand(time(0));
int r1 = rand();
srand(rand());...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |