![]() Many students will write (int)Math.random * 10, which is always 0 because Math.random() is getting cast to an int first and then multiplied. Really important thing to notice because it’s a really common mistake is that Math.random() * numberRange needs to be wrapped in parentheses before it’s cast. The result will be an integer between 1 and 10, inclusive on both ends This code will generate a random double value between 1.0 inclusive and 11.0 exclusive, and then cast it to an int value. You might write code something like this. Say that you’re flipping a coin and you want it to be heads 50% of the time and tails 50% of the time. Let’s look at a quick real world example. If there isn’t a shift value, the range starts at 0.0. That means that the range is now $ [-1.0 - 6.0) $.Īnd a quick hint… The shift value, the number we’re adding or subtracting, is always the smallest possible value in the range. In the fourth row we’re subtracting 1 from the result of Math.random() * 7. That means that the range is now $ [1.0 - 11.0) $. In the third row we’re adding 1 to the result of Math.random() * 10. ![]() In the third and fourth rows we’re shifting the range of possible values. The number we multiply gives us the upper bound, although we’ll never quite get the upper bound because Math.random() never returns 1.0. That means that the range is now $ [0.0 - 10.0) $. In the second row above we’re multiplying the result of Math.random() by 10. Let’s look at a few examples of how this works. * * Frank Yellin * 1.0 */ public class Random implements java.io.Note: If you'd like to skip ahead and just practice, scroll down to the bottom of the page. Consider instead using } to * get a cryptographically secure pseudo-random number generator for use * by security-sensitive applications. * * Instances of } are not cryptographically * secure. Consider instead using * } in multithreaded * designs. * However, the concurrent use of the same } * instance across threads may encounter contention and consequent * poor performance. * * Many applications will find the method Math#random} simpler to use. * * The algorithms implemented by class Random} use a * protected} utility method that on each invocation can supply * up to 32 pseudorandomly generated bits. However, subclasses of class Random} * are permitted to use other algorithms, so long as they adhere to the * general contracts for all the methods. Java implementations must use all the algorithms * shown here for the class Random}, for the sake of absolute * portability of Java code. In order to * guarantee this property, particular algorithms are specified for the * class Random}. (See Donald Knuth, * The Art of Computer Programming, Volume 2, Section 3.2.1.) * * If two instances of Random} are created with the same * seed, and the same sequence of method calls is made for each, they * will generate and return identical sequences of numbers. The class uses a 48-bit seed, which is * modified using a linear congruential formula. */ package java.util import java.io.* import .AtomicLong import import import import import import import import /** * An instance of this class is used to generate a stream of * pseudorandom numbers. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit if you need additional information or have any * questions. * * You should have received a copy of the GNU General Public License version * 2 along with this work if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is free software you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. Line source /* * Copyright (c) 1995, 2013, Oracle and/or its affiliates.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |