Method for a random number generator???

I need a method to generate a random number between zero and X, X being a variable set by my program. The only method I can find is Math.random(), which generates a number between 0.0 and 1.0. (Of course, I can multiply to make it an int). Can you help me out?

I dont know if this will help not but you could do this:
RandomNum = (int)Math.floor(Math.random() * X);
I think that should generate your random number between 1 and variable X
Regards,
Carl

Similar Messages

  • Defining a lower boundery for a random number Generator

    Hi,
    I am using the code:
    Random rand = new Random();
    int x = rand.nextInt(50);
    to generate a random number between 0 and 49
    However I now need to only select a number between 20 and 49
    Is there any way to give the random number generator a lower boundery as well as an upper boundery ??
    Thank you
    Craig

    example:int x = rand.nextInt(30) + 20;Will generate a number between 20 and 49.

  • Hard code a seed for a random number generator?

    Hi, I need to randomly get either 1 or 0, but in the program I'm running I want to get the same distribution of 1's and 0's every time I run it. I know I need to hardcode the seed in so I use the same seed every time, but I'm not sure how to do this. I know how to use Math.random, and just do
    double randomDouble = Math.random();
    int rand = (int)(randomDouble + 0.5);but I dont know how to hard code a seed using this.
    thanks.

    ryanj318 wrote:
    I tried that, and the problem is that I get the same random number every time (in this case I get 1 every time).Then you must be creating a new Random every time.
    To be more clear on what I want, I am generator 20 numbers, and I want a random distributions of 1's and 0's, but I want to get the same distribution every time I run the program. Like if I get 13 1's and 7 0's then I want to get that every time. Can I do this? Right now, I will end up with either 20 1's or 20 0's.
    ThanksCreate a new Random once, probably as a static member variable, but possibly just at the start of that method. Seed that Random with the same value every time it's created, and you'll get the same sequence every time you use it.
    BUT, if you create it, then use it, then create, then use, and repeat that 20 times, then you'll be getting the first number of the same sequence 20 times.
    By the way, to you want 1/0 or true/false? If the latter, I believe Random has a nextBoolean method.

  • Help: JSP Random Number Generator

    hello every one,
    Im trying to generate a random number using currentTimeMillis(), in Jsp, can you help me out and advice me how can this be done please.

    First you say you want a random number, then you say you want a unique one. Which is it?
    Random numbers do not normally guaruntee uniqueness.
    is there away i could use the currentTimeMillis(), What would you use it for? System.currentTimeMillis() is commonly used as a seed for a random number generator. The java.util.Random class uses it like that - take a look at that API link.
    Or if its a homework question state the exact requirement - I'm guessing here.

  • Random Number Generator setSeed(); method???

    I've searched and can't get a good understanding of the setSeed() method. I am suppose to use the loop control variable to seed the random number generator inside the loop. I need to use the same input everytime I use the random generator. When I compile it says int cannot be dereferenced.
    for(int i = 0; i < array.length; i++)
    int randnum = generator.nextInt(100);
    randnum.setSeed(i);
    array[i] = randnum;
    If anyone can explain the setSeed() method that would be greatly appreciated. Thanks!

    badbro wrote:
    Thanks for your help, but leave the smart comments to yourself. They are not needed. Thats what this forum is for.All of Paul's comments were dead spot on, and if you listen to him and take his advice to heart, it will only help you. If they make you upset, well then that's your problem, isn't it? My advice: grow up.

  • Help for random number generator???

    hi there can anyone how to make simple random number
    generator for slot machine , formula for chances in probability ,
    or simple random number generator for slot machine , thanks
    :)

    actualy the accurate RNG for slot machine ?? any idea?

  • Does Labview have a Random Number Generator for U16 with a periodicity greater than 64 Million instances of U16words and one with SEED as the connector?

    We are doing some testing that requires 64MegaWords be written to our DSP memory  with random values to do a validity check and without repeating sequences with using the previous value as the seed for the next and us passing it the initial seed.  The dice one doesn't meet that criteria, and I was not sure how the continuous random number generator parameters work and if any of them would meet this criteria. Anyone have any information that might help?
    Thanks,
    Sue

    Hi suem,
    There is no reason why you couldn't simply write a pseudo-random number generator by yourself. Simply select a random number algorithm you want to use and implement it in LabVIEW. You do not need to interface code written in another language in order to implement a pseudorandom number generator. If you have a DAQ card or something, you can also use inputr noise or some other hardware soruce to generate real random numbers. For that also LabVIEW is an excellent tool.
    Tomi
    Tomi Maila

  • What algorithm does Excel 2010 use for Pseudo Random Number Generation (MT19937?)

    Does Excel 2010+ use the Mersenne Twister (MT19937) algorithm for Pseudo Random Number Generation (PRNG), implemented by the RAND() function?
    This has been a nagging question for some time now, with "hints" that it indeed does.  However, a relatively thorough search turns up no definitive documentation.  The most direct indication is perhaps given by Guy Melard [Ref 9] where
    he tests Excel 2010's RAND() function using the Crush battery of tests in TestU01 by L'Ecuyer & Simard.  Melard references a "semi-official" indication that Microsoft did indeed implement MT19937 for the RAND() function in
    Excel 2010, but this reference no longer seems to be available. http://office.microsoft.com/enus/excel-help/about-solver-HP005198368.aspx?pid=CH010004571033.
    The other references below [Ref 1-10] document the history of the statistical suitability of the PRNG and probability distributions in various versions of Excel.  This includes the Wichmann-Hill PRNG implementations supposedly (arguably) used in
    Excel 2003 & 2007 for random number generation.  But still, we have no answer as to which PRNG algorithm is used in
    Excel 2010 (and 2013 for that matter).
    Microsoft indicates that RAND() has been improved in Excel 2010; Microsoft states, "...and the RAND function now uses a new random number algorithm." (see https://support.office.com/en-ca/article/Whats-New-Changes-made-to-Excel-functions-355d08c8-8358-4ecb-b6eb-e2e443e98aac). 
    But no details are given on the actual algorithm.  This is critical for Monte Carlo methods and many other applications.
    Any help would be much appreciated. Thanks.
    [Ref 1] B. McCullough, B. Wilson.  On the Accuracy of Statistical Procedures in Microsoft Excel 97. 
    Computational Statistics & Data Analysis. Vol. 31 No. 1, pp 27-37. July 1999.
    http://users.df.uba.ar/cobelli/LaboratoriosBasicos/excel97.pdf
    [Ref 2]L. Knüsel.  On the accuracy of the statistical distributions in Microsoft Excel 97. Computational Statistics & Data Analysis. Vol. 26 No. 3, pp 375-377. January 1998.
    http://www.sciencedirect.com/science/article/pii/S0167947397817562
    [Ref 3]B. McCullough, B. Wilson.  On the Accuracy of Statistical Procedures in Microsoft Excel 2000 and Excel XP. 
    Computational Statistics & Data Analysis. Vol.40 No. 4, pp 713-721. October 2002.
    https://www.researchgate.net/publication/222672996_On_the_accuracy_of_statistical_procedures_in_Microsoft_Excel_2000_and_Excel_XP/links/00b4951c314aac4702000000.pdf
    [Ref 4] B. McCullough, B. Wilson.  On the Accuracy of Statistical Procedures in Microsoft Excel 2003. 
    Computational Statistics & Data Analysis. Vol.49. No. 4, pp 1244-1252. June 2005.
    http://www.pucrs.br/famat/viali/tic_literatura/artigos/planilhas/msexcel.pdf
    [Ref 5] L. Knüsel. On the accuracy of statistical distributions in Microsoft Excel 2003. Computational Statistics & Data Analysis, Vol. 48, No. 3, pp 445-449. March 2005.
    http://www.sciencedirect.com/science/article/pii/S0167947304000337
    [Ref 6]B. McCullough, D.Heiser.  On the Accuracy of Statistical Procedures in Microsoft Excel 2007. 
    Computational Statistics & Data Analysis. Vol.52. No. 10, pp 4570-4578. June 2008.
    http://users.df.uba.ar/mricci/F1ByG2013/excel2007.pdf
    [Ref 7] A. Yalta. The Accuracy of Statistical Distributions in Microsoft<sup>®</sup> Excel 2007. Computational Statistics & Data Anlaysis. Vol. 52 No. 10, pp 4579 – 4586. June 2008.
    http://www.sciencedirect.com/science/article/pii/S0167947308001618
    [Ref 8] B. McCullough.  Microsoft Excel’s ‘Not The Wichmann-Hill’ Random Number Generators. Computational Statistics and Data Analysis. Vol.52. No. 10, pp 4587-4593. June 2008.
    http://www.sciencedirect.com/science/article/pii/S016794730800162X
    [Ref 9] G. Melard.  On the Accuracy of Statistical Procedures in Microsoft Excel 2010. Computational Statistics. Vol.29 No. 5, pp 1095-1128. October 2014.
    http://homepages.ulb.ac.be/~gmelard/rech/gmelard_csda23.pdf
    [Ref 10] L. Knüsel.  On the Accuracy of Statistical Distributions in Microsoft Excel 2010. Department of Statistics - University of Munich, Germany.
    http://www.csdassn.org/software_reports/excel2011.pdf

    I found the same KB article:
    https://support.microsoft.com/en-us/kb/828795
    This was introduced (according to the article) in Excel 2003. Perhaps the references in notes 2 and 3 might help.
    The article describes combining the results of 3 generators, each similar to a Multiply With Carry (MWC) generator, but with zero carry. MWC generators do very well on the Diehard battery of randomness tests (mentioned in your references), and have
    very long periods. But using zero carry makes no sense to me.
    Combining the three generators only helps if the periods of the 3 are relatively prime (despite what the article implies). Then the period of the result will be the product of the 3 periods. But without knowing the theory behind these generators, I have
    no idea what the periods would be. The formulas for MWC generators fail here.
    Richard Mueller - MVP Directory Services

  • Java random number generator

    Hi does java have a random number generator. If it does can you point me to it please. Thank you.

    you can use "java.Math.random()" this function returns a value which is greater than or equal
    to 0.0 and less than 1.It returns a double value,so you can multiply by 10 and cast it to int,so that it
    can produce numbers between 0 & 9.Yes, you can do that. But, as stated before, java.util.Random has methods that are easier to use for random int values than using java.Math.random is for random int values. It all depends what the OP wants. The OP hasn't come back with additional questions, or to say that he found his answer. So, we don't know whether he has his answer (he has several options now!) or not. :)

  • Random number generator. Even and odd numbers

    I have a problem.
    I have a random number generator, it goes from 0 to 20, I need to have two indicators, one will show how many numbers are pairs, and how many are not.
    I tried to use the decimated 1D array, but since those numbers are random, it doesn´t work for me. I´m new with LabView, and I don´t know if there is some other way to make it work, I need some help, especially with some examples.
    Thank you so much.
    Solved!
    Go to Solution.

    RavensFan wrote:
    Spoiler (Highlight to read)
    Create the array.  Use quotient remainder to divide by 2.  Now you have an array where all the odd numbers are now 1, and all the even numbers are now 0.  Sum the array and you have the total number of odd numbers.  The number of evens will be the length of the array divided by the # of odds.
    Create the array.  Use quotient remainder to divide by 2.  Now you have an array where all the odd numbers are now 1, and all the even numbers are now 0.  Sum the array and you have the total number of odd numbers.  The number of evens will be the length of the array divided by the # of odds.
    Haha I lost sight of the fact that he was looking for the amount off odds and evens.  Good one.
    Bill
    (Mid-Level minion.)
    My support system ensures that I don't look totally incompetent.
    Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.

  • Random NUMBER generator help..

    Hi all,
    i'm having issues with my random number generator i'm trying to fill my array with random numbers but i keep getting it filled with zeros.... can any one tell me what i'm doing wrong i can;t figure it out..
    for (int i =0; i < 100; i++){
                        int x=(int) (Math.random());
                        data[i] = x;
    }

    From the API "Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0."
    So casting the value to int is always going to result in a zero value.
    You need to multiply by a value that is one larger than the value you want to be your maximum. For example:
    int x = (int)(Math.random() * 100);
    should create a random number from 0 to 99

  • How do I create a random number generator?

    I know I've done it before...but I can't remember quite how to get it going. I'm creating a math tutor program, which generates two random numbers and asks the user to add them, and if the answer is right they get a message that gives props and if they get it wrong they're told they got it wrong.
    I can't get too far because I've forgotten how to make the number generator, so what exactly do you put in to get it?
    import java.util.*;
    public class AssignmentFour3 {
         private static Random integer = new Random();
         double number1 = 0;
         Random string1 = new Random();
         }

    DrClap wrote:
    Generally I don't write my own random number generator. I just use the java.util.Random class.I read it as though that that is what he was going to use, but wasn't able to read the API docs to figure out what methods to use. ;-)

  • Uniform random number generator

    anyone of u know how to generate UNIFORM RANDOM NUMBER GENERATOR that has serious faults ?

    jwenting wrote:
    uj_ is right (never thought I'd say that).
    ANYTHING "uniform" is by definition not random.Well, the correct term is "random numbers from a uniform distribution". And there certainly exist such generators (athough they're pseudo-random of course).
    I lost my temper because I've spent considerable time helping sosys only to realize he's a notorious cheater. Sosys claims to be a college student attending a CS class. With the knowledge, or rather lack thereof, he's displaying he must've cheated systematically for a long long time. I'm quite liberate at helping people with their homework but I won't help a leech become a programmer.

  • Favorite random number generator.

    As I hope that not everyone is using the core java API for generating random ints, floats, etc., I was curious what RNG (random number generators) people are using. I am currently using the Mersenne Twister Fast, and have glanced at the RNG in the Colt API. What do you use? What is your favourite RNG?

    I also favor this crisp random number generator, which is adapted to Java from the book "Numerical Recipes in C++".
    http://www.nr.com/cpp-blurb.html
    public class Ran {
                  private int idum;
                 final static int IA = 16807;
         final static int IM = 2147483647;
         final static int IQ = 127773;
         final static int IR = 2836;
         final static int MASK = 123459876;
         final static double AM = 1.0 / (double)IM;
                Ran(){
                    this.idum = (int)(System.currentTimeMillis()%262144L);
                Ran(int n){
                    this.idum = n;
        final public double rand(){
         this.idum ^= MASK;
         int k = this.idum / IQ;
         this.idum = IA*(this.idum - k*IQ) - IR*k;
         if(this.idum < 0){this.idum += IM;}
         double ans = AM*this.idum;
         this.idum ^= MASK;
         return ans;
    }(This is a privately converted version, not published.)

  • Support for hardware random number generators?

    Can a hardware random number generator be easy integrated into Solaris so that it is accessed via /dev/urandom or /dev/random?
    Hardware Random Number Generators such as (just googling "usb hardware random number generator")
    http://true-random.com/
    http://www.protego.se/
    http://www.araneus.fi/products-alea-eng.html
    http://www.westphal-electronic.de/zranusbe.htm
    Is it possible to have a generic framework which can work with all these devices?

    This is a forum specifically for Java, not Sun products in general. You might try one on the Solaris forums.

Maybe you are looking for

  • What is the difference when you enroll in your bank's on line bill payment program?

    This is really more of a general question that doesn't specifically have anything to do with Verizon Wireless.  Somebody here mentioned that it's possible to have your bank involved in paying your bills on line?  What exactly does that involve?  Does

  • Using an external cd-rom  for  re-installing  the os X?

    hello, i have a problem , my superdrive it`s not working, and i don`t want to replace the unit, i have an external cd-rom an want to know if i can re-installing or re-boot it from the external drive. please help me,... i need to errase evrything from

  • How can I debug leaks caused by ODBC calls in app linked with libtt.so?

    Hi, we are using TimesTen Release 7.0.5.0.0 on Solaris 10. and we are using libtt.so to link our application and connect using ODBC calls. The problem is that we have made some serious changes to our large code base and now we see a leak. Here is wha

  • Firefox, Adium Issues

    Whenever I start my MacBook Pro, iMac or my wife starts her MacBook Pro. Once we launch Firefox says its not the default browser and asks if we want to make it the default browser and we select Yes but it does it every single time like its not saving

  • Album Pre-Order from Phone to Phone

    I pre-ordered Jason Mraz's "Yes!".  Every Monday a new song downloaded on my old iphone.  Now that I have a new one (same as before 5S), it doesn't automatically put the song in my itunes.  When I click, "complete my album" it shows it has been pre-o