Java Forum / First Aid / October 2008
random double between -6.0 and 6.0
bH - 25 Oct 2008 02:52 GMT Hi All,
Given a task of writing a random double between -6.0 and 6.0 and the sample code below, is there a way to write it in a more compact way i.e fewer lines of code? I am looking at Roedy's web site, for clues <http://mindprod.com/jgloss/pseudorandom.html#SEEDING> but none quite match. TIA, bH
import java.util.Random;
public class GenerateRandom{ static Random wheelDbl = new Random(); static Random wheelNum = new Random(); static int intNumber = 0; static double dNumber = 0.0; static double desiredNum = 0.0;
static int low = 0; static int high = 0;
public static void main(String[] args) { GenerateRandom GenerateRandom1 = new GenerateRandom(); }
public GenerateRandom() { for (int i= 0;i<100;i++){ dNumber = wheelDbl.nextDouble() * (6d); low = 1; high = 2; intNumber = wheelNum.nextInt(high - low + 1) + low;
if ( intNumber == 1) { desiredNum = - 1 * dNumber;} if ( intNumber == 2) { desiredNum = 1 * dNumber;}
System.out.println( desiredNum ); } } }
Knute Johnson - 25 Oct 2008 04:31 GMT > Hi All, > [quoted text clipped - 39 lines] > } > } The answer is yes!
Random r = new Random(System.nanoTime()); double d = r.nextDouble() * 12.0 - 6.0;
 Signature Knute Johnson email s/nospam/knute2008/
Daniel Pitts - 25 Oct 2008 04:49 GMT >> Hi All, >> [quoted text clipped - 44 lines] > Random r = new Random(System.nanoTime()); > double d = r.nextDouble() * 12.0 - 6.0; <pedantic> Those are not functionally the same. The OP version will return values in the range of (-6.0, 6.0) exclusive, with a bias to 0. Your version will return a value in the range of [-6.0, 6.0), with no bias. </pedantic>
My guess, the later is more desirable anyway, and the in either case, the "chances" of a pure zero are so remote that it won't make a difference in the "average" application.
Also, a little obligatory note about the danger of using non-final statics the way the OP has. Don't do it unless you know what you're doing and why. Chances are you don't know, so don't do it. If you /think/ you know why, then still try to find another way.
Good luck, Daniel.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Knute Johnson - 25 Oct 2008 05:04 GMT >>> Hi All, >>> [quoted text clipped - 62 lines] > Good luck, > Daniel. Daniel:
I don't see the bias, can you explain?
Thanks,
 Signature Knute Johnson email s/nospam/knute2008/
Joshua Cranmer - 25 Oct 2008 05:17 GMT > I don't see the bias, can you explain? Suppose I generate 0.25 from the double. Half the time (when, that becomes -0.25. Now suppose I generate 0 from the double. Half the time, that 0 becomes... 0. So 0 is twice as likely to occur as 0.25 and -0.25 since it's not being changed half the time.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Knute Johnson - 25 Oct 2008 18:47 GMT >> I don't see the bias, can you explain? > > Suppose I generate 0.25 from the double. Half the time (when, that > becomes -0.25. Now suppose I generate 0 from the double. Half the time, > that 0 becomes... 0. So 0 is twice as likely to occur as 0.25 and -0.25 > since it's not being changed half the time. OK. I see that. But it is only 0 that will be more likely to occur, not values close to 0. I read Daniel's comment to be biased towards 0 and not what he actually said, "biased to 0."
I'm trying a test to see how many times 0 actually comes up from the random number generator. I'll report back later.
Thanks,
 Signature Knute Johnson email s/nospam/knute2008/
Juha Laiho - 30 Oct 2008 19:37 GMT Knute Johnson <nospam@rabbitbrush.frazmtn.com> said:
>I'm trying a test to see how many times 0 actually comes up from the >random number generator. I'll report back later. I guess it should come up a random number of times, no?
(sorry, I just couldn't resist)
 Signature Wolf a.k.a. Juha Laiho Espoo, Finland (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++ "...cancel my subscription to the resurrection!" (Jim Morrison)
Knute Johnson - 30 Oct 2008 19:48 GMT > Knute Johnson <nospam@rabbitbrush.frazmtn.com> said: >> I'm trying a test to see how many times 0 actually comes up from the [quoted text clipped - 3 lines] > > (sorry, I just couldn't resist) Actually, I tried millions of samples and it never came up. I thought that was curious but I haven't had time to look into it any more.
 Signature Knute Johnson email s/nospam/knute2008/
Patricia Shanahan - 30 Oct 2008 20:50 GMT >> Knute Johnson <nospam@rabbitbrush.frazmtn.com> said: >>> I'm trying a test to see how many times 0 actually comes up from the [quoted text clipped - 6 lines] > Actually, I tried millions of samples and it never came up. I thought > that was curious but I haven't had time to look into it any more. Random's nextDouble has 2**53, about 9e15, possible results, all supposed to be equally likely, so the probability of 0 on each trial should be about 1.1e-16.
I tried using Excel's BINOMDIST to calculate the probability of at least one success for various number of trials. Unfortunately, it got into overflow problems. It did report 0.00000011102229635629 for 1e9 trials, so it would actually be curious if you did find 0, or any other specific, pre-selected result, in a few million samples.
Patricia
Knute Johnson - 30 Oct 2008 21:52 GMT >>> Knute Johnson <nospam@rabbitbrush.frazmtn.com> said: >>>> I'm trying a test to see how many times 0 actually comes up from the [quoted text clipped - 18 lines] > > Patricia Thanks for the info Patricia, that is really what I wanted to know about the random number generator but didn't know how to get. I just tried about 3 billion numbers and did not get a hit. What I would like to do is create a program that would confirm these numbers. I'll probably have to leave my computer running for few days :-).
 Signature Knute Johnson email s/nospam/knute2008/
Eric Sosman - 31 Oct 2008 02:29 GMT >>>> Knute Johnson <nospam@rabbitbrush.frazmtn.com> said: >>>>> I'm trying a test to see how many times 0 actually comes up from [quoted text clipped - 25 lines] > is create a program that would confirm these numbers. I'll probably > have to leave my computer running for few days :-). "A few days?" Make that "a few months" if you can generate and test a billion numbers per second; somewhat longer if you don't happen to have a 50GHz machine handy.
2^53 numbers = 9007199254740992 numbers / 1E9 numbers/s ~= 9007199 seconds / 3600 s/hr ~= 2502 hours / 24 hr/day ~= 104 days / 30.4 day/mo ~= 3.4 months
Fortunately for you, there's a 50% chance you'll hit the target number in just half that time.
 Signature Eric Sosman esosman@ieee-dot-org.invalid
Knute Johnson - 31 Oct 2008 02:58 GMT >>>>> Knute Johnson <nospam@rabbitbrush.frazmtn.com> said: >>>>>> I'm trying a test to see how many times 0 actually comes up from [quoted text clipped - 38 lines] > Fortunately for you, there's a 50% chance you'll hit the target > number in just half that time. I understand that, it was more of a joke than a statement that I was really going to generate 9 quadrillion random numbers. I can generate about 2 billion random numbers on my computer in about two minutes. I need to think about a multi-threaded approach to maximize my computing power :-).
 Signature Knute Johnson email s/nospam/knute2008/
Lew - 25 Oct 2008 05:15 GMT bH wrote:
>>> import java.util.Random; >>> [quoted text clipped - 27 lines] >>> } >>> }
> Also, a little obligatory note about the danger of using non-final > statics the way the OP has. Don't do it unless you know what you're > doing and why. Chances are you don't know, so don't do it. If you > /think/ you know why, then still try to find another way. Also, also, do *not* do all the work in the constructor!
Constructors are for construction.
Turn your static variables into instance variables, or better, local variables, some of them. Turn the constructor into a (different-named) method. Invoke the method from a 'new' instance of the class constructed in a 'main()' method.
 Signature Lew
Mark Space - 26 Oct 2008 02:08 GMT I was going to say the exact same thing as Lew. Using a constructor to implement some algorithm is sloppy at best, and likely to be seen as a serious design flaw by anyone else reading your program. (E.g., your teacher, or your boss.)
If you find yourself needing to do something, and you can't think of any reason to have it do anything outside of one constructor, consider a static method instead:
> import java.util.Random; > > public class GenerateRandom{
> public static void main(String[] args) { generateRandom();
> } private static void generateRandom() { // Using Knute's solution... Random r = new Random(System.nanoTime()); for( int i=0; i<100; i++ ) { System.out.println( r.nextDouble() * 12.0 - 6.0 ); } }
> } I didn't compile that, but I think you get the idea.
Roedy Green - 27 Oct 2008 09:46 GMT >Given a task of writing a random double between -6.0 and 6.0 >and the sample code below, >is there a way to write it in a more compact way >i.e fewer lines of code? >I am looking at Roedy's web site, for clues see http://mindprod.com/jgloss/pseudorandom.html#LOWHIGH The exact code you need is there.
 Signature Roedy Green Canadian Mind Products http://mindprod.com The Canadian national animal should be changed from the beaver to the ostrich. Canadians elected a party that denies global warming so they too could pretend it presents no danger.
alwin - 28 Oct 2008 12:33 GMT > Given a task of writing a random double between -6.0 and 6.0 this should do the trick :
Math.random()*12.0-6.0
alwin
 Signature ' 1 == 1; --
bH - 28 Oct 2008 20:45 GMT > > Given a task of writing a random double between -6.0 and 6.0 > [quoted text clipped - 6 lines] > -- > ' 1 == 1; -- Hi All, Any of these three models would satisfy me. Thanks to all.
bH
import java.util.Random;
public class GenerateRandomV0{ public static void main(String[] args) { generateRandom(); }
private static void generateRandom() { //Using another possible solution Random r = new Random(System.currentTimeMillis()); // Or Using Knute's solution...(uncomment if to be checked) // Random r = new Random(System.nanoTime()); double aRis = 0; for( int i=0; i<100; i++ ) { aRis = r.nextDouble() * 12.0 - 6.0; System.out.println("number is:"+ aRis); } } }
import java.util.*;
// <http://mindprod.com/jgloss/pseudorandom.html#LOWHIGH> // modified to show shuffle public class GenerateRandomV1{ public static void main(String[] args) { generateRandom(); } private static void generateRandom() { Random wheel = new Random(); double rndDouble= 0.0; String arrayStrRndIDbl[] = new String [100];
// generate a number between -6.0 <= x < 6.0, // then scale then shuffle for ( int i=0; i<50; i++ ) { //all positive values rndDouble = wheel.nextDouble() * 6.0d; arrayStrRndIDbl[i] = Double.toString(rndDouble); } for ( int i=50; i<100; i++ ) { // all negative values rndDouble = wheel.nextDouble() * -6.0d; arrayStrRndIDbl[i] = Double.toString(rndDouble); } List <String> inputList = Arrays.asList(arrayStrRndIDbl); Collections.shuffle(inputList); for(String s: inputList){ double dblNum = Double.parseDouble(s); System.out.println(" a number = "+ dblNum); }
} }
// using alwin's gift // no import to install
public class GenerateRandomV3{
public static void main(String[] args) { generateRandom(); }
private static void generateRandom() { double aRis = 0;// a Random is for( int i=0; i<100; i++ ) { aRis = Math.random()*12.0-6.0; System.out.println(aRis); } } }
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|