Random Value Generation

FYI, time to give back. I have received a number of excellent
responses from this forum, so here is one that might help someone
else.
I had found and modified the code below to generate a random
value (letters and numbers) to use as a member ID. With it you can
set the length of the generated value, as well as the content
(numbers, capital letters, lower case letters, even special
symbols). I chose a subset of capital letters along with 0-9 so the
result contained more numbers.

I don't get it?! Why not just use something like
#createUUID()#, which is also apparently meant to be unique
everytime.
If you don't like the format of createUUID you can always run
a couple of functions over it to alter it a bit, like taking out
some of the dashes, or making it longer etc.
Maybe I'm missin the point...it's late here!! Me =
sleepy.

Similar Messages

  • How can i get the random values from database?

    Hi,
    i want to get random values from database.
    I try my best find no solution
    plz give solution in either sql query or java method.
    thanks in advance.

    try this:
    Give a numeric row-id to each row of database.
    say (1-100) for 100 rows
    In the program use random function to get random number between 0 and 1. this value u multiply with 100(or total number of rows) and take integer value of it . u then perform sql query to select the a row which matches randomly genarated value with row-id assigned to each row of database
    madhu

  • A simple question on random number generation?

    Hi,
    This is a rather simple question and shows my newbieness quite blatantly!
    I'm trying to generate a random number in a part of a test I have.
    So, I have a little method which looks like this:
    public int getRandomNumber(int number){
            Random random = new Random(number);
            return random.nextInt(number);
        }And in my code I do int random = getRandomNumber(blah)...where blah is always the same number.
    My problem is it always returns the same number. What am I missing here. I was under the impression that nextint(int n) was supposed to generate the number randomly!! Obviously I'm doing something wrong or not using the correct thing. Someone please point out my stupidity and point me in the right direction? Ta

    I think the idea is that Random will generate the same pseudo-random sequence over and over if you don't supply a seed value. (The better to debug with, my dear.) When you're ready to put an app into production, the seed value should be the current system time in milliseconds to guarantee a new sequence with each run.
    Do indeed move Random outside the loop. Think of it like a number factory - instantiate it once and let it pump out the random values for you as needed.

  • 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

  • Selecting random values from an array based on a condition

    Hi All,
    I have a small glitch here. hope you guys can help me out.
    I have an evenly spaced integer array as X[ ] = {1,2,3, ....150}. and I need to create a new array which selects 5 random values from X [ ] , but with a condition that these random values should have a difference of atleast 25 between them.
    for example res [ ] = {2,60,37,110,130}
    res [ ] = {10,40,109,132,75}
    I have been thinking of looping thru the X [ ], and selecting randomly but things look tricky once I sit down to write an algorithm to do it.
    Can anyone think of an approach to do this, any help will be greatly appreciated ...

    For your interest, here is my attempt.
    import java.util.Random;
    public class TestDemo {
        public static void main(String[] args) throws Exception {
            for (int n = 0; n < 10; n++) {
                System.out.println(toString(getValues(5, 25, 150)));
        private final static Random RAND = new Random();
        public static int[] getValues(int num, int spread, int max) {
            if (num * spread >= max) {
                throw new IllegalArgumentException("Cannot produce " + num + " spread " + spread + " less than or equals to " + max);
            int[] nums = new int[num];
            int max2 = max - (num - 1) * spread - 1;
            // generate random offsets totally less than max2
            for (int j = 0; j < num; j++) {
                int next = RAND.nextInt(max2);
                // favour smaller numbers.
                if (max2 > spread/2)
                    next = RAND.nextInt(next+1);
                nums[j] = next;
                max2 -= next;
            // shuffle the offsets.
            for (int j = num; j > 1; j--) {
                swap(nums, j - 1, RAND.nextInt(j));
            // add the spread of 25 each.
            for (int j = 1; j < num; j++) {
                nums[j] += nums[j-1] + spread;
            return nums;
        private static void swap(int[] arr, int i, int j) {
            int tmp = arr;
    arr[i] = arr[j];
    arr[j] = tmp;
    public static String toString(int[] nums) {
    StringBuffer sb = new StringBuffer(nums.length * 4);
    sb.append("[ ");
    for (int j = 0; j < nums.length; j++) {
    if (j > 0) {
    sb.append(", ");
    sb.append(nums[j]);
    sb.append(" ]");
    return sb.toString();

  • Weighted random value (1-10) e.g.3 will be  50 % of records

    Hello,
    i need to generate random values into my table. For the simplification, lets say that i need to generate values in range from 1 to 10. For this there is a lot of ways.
    e.g. UPDATE test_number SET random_value = round (dbms_random.value (1,10))
    But what I need is something like weighted random values. I need random values to be inserted into column, but I need e.g. 3 to be inserted more times than other values e.g. if i have 100 rows i want to have 3 in 50 random rows
    Do you have any idea how to achieve it?
    Thanks a lot for any help or reply !

    Hi,
    You can do something like this:
    WITH     got_p_num     AS
         SELECT     LEVEL                         AS id
         ,     FLOOR (dbms_random.value (1, 19))     AS p_num
         FROM     dual
         CONNECT BY     LEVEL     <= 10
    SELECT       id
    ,       p_num
    ,       CASE
              WHEN  p_num     < 3     THEN p_num
              WHEN  p_num     < 12     THEN 3
                                     ELSE p_num - 8
           END     AS r_num
    FROM       got_p_num
    ORDER BY  r_num
    ;If you want the number 3 to appear on half of the rows; that means that, out of 18 random numbers, you expect 3 to occur 9 times, and the other numbers to appear once each. The query above generates random integers in the range 1 through 18 inclusive, and calls this number p_num. The CASE statement maps p_num to what you really want, a n integer between 1 and 10, with 3 occurring 9/18 (= 1/2) of the time.
    Instead of using a CASE expression, you might want to create a table, that has one row for every number that can be in the output, and the probability of picking that number. You could then join to that table to convert the raw random number to the id that you want.
    Edited by: Frank Kulash on Oct 6, 2011 3:36 PM

  • Random email generation

    i need the source code of random email generation.

    Can explain How you want the random email , or you want any unrepeated emails?

  • Inserting Random Values

    Hi there!
    just a little question:
    I want to insert into a table random values between -2 and 2, which shoul differ for every row. When I'm writing
      FOR n IN nodes LOOP
        FOR EACH n2 IN nodes2 LOOP
          IF (n2.layer_ = n.layer_ +1) THEN
            INSERT INTO nn2_links (lfdnr, sourceid, destid, weight, delta_weight, Zone_)
                 VALUES (lfdnr#, n.nodeid, n2.nodeid, dbms_random.value(-2,2), 0, Zone#);
            lfdnr# := lfdnr# +1;
          END if;
        END LOOP;
      END LOOP; I'll get the same value for all records fullfilling the IF-criteria in the middle. What can I do? Must I write another loop?
    greetings
    ~Mel

    OP states that she / he would like to have distinct values for every row but instead gets the same value for all rows.
    To OP:
    Sorry mel, not much help. Can't see the error in your script.
    When I put a anonymous block together, it runs smoothly:
    SQL> set serveroutput on
    SQL> declare
      2  outn number;
      3  cnt number;
      4  begin
      5  cnt := 1;
      6  while cnt <=10 loop
      7     select dbms_random.value(-2,2) into outn from dual;
      8     dbms_output.put_line('Wert: '|| to_char(outn));
      9     cnt := cnt + 1;
    10  end loop;
    11  end;
    12  /
    Wert: 1,94760933757912513703045869459081746304
    Wert: ,1850733416444459069798272966494986884
    Wert: -1,28557676353717597230879638125649445416
    Wert: -,20789721958144010481059709280045566588
    Wert: -1,46550435548323732535026606603236048092
    Wert: -1,48594688953909777344516402506798302304
    Wert: -,2162809554864053530960985675499170964
    Wert: 1,71049071188915501435899411045229762652
    Wert: ,53354523369664937673204584890644358732
    Wert: -,96917849158672171927090500710866039384
    PL/SQL-Prozedur wurde erfolgreich abgeschlossen.
    SQL>

  • Unique Random Values

    OK, firstly, I am a Java n00b. I am not creating a program, just editing some source, but I need some help. What I need, are 5 different variables, each with a random value (integer) between 1 and 5 (inclusive), but, I don't want to variables to have the same value. The outcomes I'm looking for could be as follows:
    t = 4
    i = 3
    g1 = 5
    g2 = 1
    y = 2
    and then the next time the code is run I might get:
    t = 1
    i = 5
    g1 = 4
    g2 = 2
    y = 3
    Each variable has a random value (integer) between 1 and 5 (inclusive), but no 2 variables have the same value.
    Here is what I tried:
         Random rand = new Random();
         int t = (rand.nextInt(5) + 1);
         int i = (rand.nextInt(5) + 1);
         int g1 = (rand.nextInt(5) + 1);
         int g2 = (rand.nextInt(5) + 1);
         int y = (rand.nextInt(5) + 1);
    // While i is the same as t, make i a new random integer between 1 and 5. Yes? Or no...?
         do {
         i = (rand.nextInt(5) +1);
         } while (i == t);
         do {
         g1 = (rand.nextInt(5) +1);
         } while (g1 == t);
         do {
         g1 = (rand.nextInt(5) +1);
         } while (g1 == i);
         do {
         g2 = (rand.nextInt(5) +1);
         } while (g2 == t);
         do {
         g2 = (rand.nextInt(5) +1);
         } while (g2 == i);
         do {
         g2 = (rand.nextInt(5) +1);
         } while (g2 == g1);
         do {
         y = (rand.nextInt(5) +1);
         } while (y == t);
         do {
         y = (rand.nextInt(5) +1);
         } while (y == i);
         do {
         y = (rand.nextInt(5) +1);
         } while (y == g1);
         do {
         y = (rand.nextInt(5) +1);
         } while (y == g2);But when I use the variables in the next part of the program, sometimes it works, but sometimes 2 of them are the same. Sometimes even 4...
    Can someone help me please?

    Thinking about it now though, I would like to learnproperly. What is a good Java guide that starts from
    scratch?
    I think the Core Java books, by Cay Horstmann, are a
    great way to start learning Java. I refered to his
    books constantly durning college:
    http://www.amazon.com/exec/obidos/tg/detail/-/013148202
    /qid=1096410169/sr=1-1/ref=sr_1_1/104-3324308-3016709?v
    glance&s=books
    Sun's basic Java tutorial
    Sun's New To Java Center. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
    http://javaalmanac.com. A couple dozen code examples that supplement The Java Developers Almanac.
    jGuru. A general Java resource site. Includes FAQs, forums, courses, more.
    JavaRanch. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.
    Bruce Eckel's Thinking in Java (Available online.)
    Joshua Bloch's Effective Java
    Bert Bates and Kathy Sierra's Head First Java.

  • Get a random value out of a query result

    I have a query:
    select id
    from class_schedule
    where id not in (
    select distinct(event_id) from trainer_schedule)I want to pick up a random value from this query result and assign it to a varible e.g. randomID.
    please help

    here is the example
    SQL> SELECT empno,ename
      2  FROM (
      3    SELECT empno,ename
      4    FROM EMP
      5    ORDER BY dbms_random.VALUE)
      6  WHERE ROWNUM = 1;
          7876 ADAMS
    SQL> /
          7566 JONES
    SQL> /
          7900 JAMES
    SQL> /
          7844 TURNER
    SQL>

  • Select statement for JDBC receiver synch scenario for capturing random value from ECC portal

    Dear Experts,
    I am working on ECC <----> SAP-PO 7.31 <----> JDBC synchronous scenario. I am clear about the config part except the Select statement. I will be
    capturing 2 random values from the portal i.e. VendId and VendName in ECC to get the vendor details like Vendor Country, Vendor Status, Vendor Contact , Vendor Address etc from JDBC vendor table/view VENDETAIL.
    What would be the select statement to capture the random values for ECC portal? My select statement would look some thing like this..
    Select f1,f2,f3,f4 from table VENDETAIL where key1 = "VendId" and "VendName"
    Please suggest if the above select statement works for the above scenario...
    Regards
    Rebecca

    Hi Rebecca,
    Your statement should work fine.
    Please see the statement we use below.
    SELECT eT_cashier, eT_proc_yn, eT_proc_date FROM eTest WHERE eb_proc_yn = 'N'
    Just remember to update the change indicator so that you dont duplicate your records.
    UPDATE eTest SET eb_proc_yn = 'Y' WHERE eb_proc_yn = 'N'.
    Regards,
    Jannus Botha

  • Build array from random values via ethernet

    Am trying to save data that comes from ehternet ((random values (modbus protocol)) in an array.
    I am using an index array.....but all the values are the same!! How can I sort them??
    It functions correctly with a for loop with random numbers build.......but it doesn't work with the data from ethernet.
    Can someone please help me??
    Attachments:
    Data.vi ‏16 KB

    chendel wrote:
    Am trying to save data that comes from ehternet ((random values (modbus protocol)) in an array.
    I am using an index array.....but all the values are the same!!
    You just read the same shared variable 100 times within a few nanoseconds. Apparently, the variable gets modified less often on the other end.
    chendel wrote:
    How can I sort them??
    If all array values are the same, they are already sorted.
    You need to rethink your code. maybe you can make the shared variable an array and write the entire random array on the other end, the read it out once.
    LabVIEW Champion . Do more with less code and in less time .

  • True Random number generation in OSX

    Hi there,
    I have been trying to find some information on how to access the Intel Random number generation hardware on my Mac.
    First I would like to understand which models currently offer this, second I would like to know if there is some library available I could use from either C or any other language, even from Applescript would be good for me.
    Thanks in advance.

    Perhaps these links help:
    http://www.intel.com/software/products/mkl/docs/mklnotes_mac.htm
    (keep in mind that apple typically discourages targeting specific hardware...)
    http://forums.macosxhints.com/showthread.php?t=46359
    http://www.mactricksandtips.com/2008/03/apple-script-random-number-generator.htm l

  • Global random integer generation in a range

    Hello,
    I need a way to achieve a global random integer generation in a range.
    I tried FMs QF05_RANDOM_INTEGER and RANDOM_I4 without success because every time I call them from an object, always get the same number
    For example, FM RANDOM_I4 with RND_MIN = 1 and RND_MAX = 5 gets RND_VALUE = 1 and FM QF05_RANDOM_INTEGER with RAN_INT_MAX = 5 and AN_INT_MIN = 1 gets RAN_INT = 3.
    How can I get a global (with memory) random number generator?
    Regards and thanks in advance.

    Hey run the code below, it will give you different numbers.
    data RAN_INT like QF00-RAN_INT.
    data G_RAN_SEED like QF00-RAN_SEED.
    CALL FUNCTION 'QF05_RANDOM_INTEGER'
        EXPORTING
             RAN_INT_MAX   = 5
             RAN_INT_MIN   = 1
        IMPORTING
             RAN_INT       = RAN_INT
        EXCEPTIONS
             INVALID_INPUT = 1
             OTHERS        = 2
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    write ran_int.
    G_RAN_SEED = ran_int.
    CALL FUNCTION 'QF05_RANDOM_SAVE_SEED'
        EXPORTING
             RAN_SEED_NEXT = G_RAN_SEED

  • Problem with random number generation

    hey forum, i wonder if anyone can help me out with a small problem with my university coursework (yep its homework!!!)
    heres my problem, i am writing one of them 8 puzzle problems (the one based around sam lloyds 15 puzzler), i can successfully generate one random sequence of numbers with no duplicates, but whenever i call the random method again it keeps producing the same results
    heres the code
    in my main class file
    if(e.getSource() == randomButton) {
          new RandomPuzzle();
          System.out.println(random.randState);
          //startStateString = new RandomPuzzle();
    }heres my number generator class file
    import java.util.Random;
    import java.io.*;
    public class RandomPuzzle
         /** Base Random number generator */
        Random rn = new Random();
        /** Puzzle holder */
        byte b[];
        long number = 0;
        String randState = "";
        /** Default constructor */
        public RandomPuzzle() {
            rn.setSeed(number);
            b = new byte[9];
            randState = randomString();
        /** Provide range for generation */
        public int rand(int lo, int hi) {
            int n = hi - lo + 1;
            int i = rn.nextInt() % n;
            if (i < 0)
            i = -i;
            return lo + i;
        /** Set size for array */
        public int rand( int hi){
            int n = hi;
                return n;
        /** Check for duplicate values within the same configuration */
        boolean valueExists( byte value ) {
            int i = b.length;
            boolean exists = false;
            for( int j = 0; j < i; j++ ){
                if( b[j] == value )
                    exists = true;
            return exists;
        /** returns the actual string */
        public String randomString(int lo, int hi) {
            int n = rand( 9 );
            //boolean valueEntered = false;
            for (int i = 0; i < 9; i++) {
                boolean valueEntered = false;
                byte temp = (byte)rand('0', '8');
                while( valueEntered == false ) {
                    if( !valueExists( temp ) ) {
                         b[i] = temp;
                         valueEntered = true;
                    else
                        temp = (byte)rand('0', '8');
            return new String(b, 0);
        /** calls above function */
        public String randomString() {
            return randomString(0, 8);
    }i've tried for hours to work this out, but i am stumped. if anyone can point me in the right direction, maybe point out the problem code and give one or two tips i would be forever in your debt
    thanx in advance
    korbitz

    thanx for the help paulcw, but when i removed the seed code it done the same
    but i added this to my main class and now it works fine
    if(e.getSource() == randomButton) {
                   RandomPuzzle temp = new RandomPuzzle();
                   System.out.println(temp.randState);
                   //startStateString = new RandomPuzzle();
              }thanx again for your help

Maybe you are looking for