Select 3 Unique random numbers of a Recordset

I have a table, a row with 3 columns. I want to put a random item I have a database in each column, but want the 3 to be unique. I have a code that runs and runs, but his does not work exactly perfect.

The following code generates 3 unique pieces of data, but view the first item is always the same, and sometimes it will generate 2 points instead of 3. The table has 5 elements.

What should I do to change my code so that it displays 3 ads every time, and that all 3 ads are random, unique and not a copy?

< CFOUTPUT >

< name cfquery "ri" = >

SELECT *.

Oeitem I have

LEFT JOIN OEVendorCoupons c

ON i.itempromocode = c.couponid

WHERE i.itemsaleprice > 0 AND c.coupon_exp > = GetDate()

< / cfquery >

< CFSET itemlist = "" > "".

< CFSET itemcount = 0 >

< CFSET displaylist = "" > "".

< request CFLOOP "ri" = >

< CFSET itemlist = ListAppend (itemlist, ri.itemid) >

< / CFLOOP >

< CFSET ilistcount = ListLen (itemlist) >

< list CFLOOP = "" #itemlist # "index ="item">"

< CFSET randomi = RandRange(1,ilistcount) >

< CFIF NOT ListContains (displaylist, ListGetAt (itemlist, randomi)) >

< CFSET displaylist = ListAppend(displaylist,item) >

< CFSET itemcount = itemcount + 1 >

< / CFIF >

< / CFLOOP >

< table width = "650" height = "315" border = "0" cellspacing = "0" cellpadding = "2" >

< b >

< CFSET outputcount = 0 >

< list CFLOOP = "' #displaylist # ' index 'i' = >"

< CFSET outputcount = outputcount + 1 >

< CFIF outputcount LT 4 >

"< cfquery name ="If"datasource =" "#DSN #" username = "#USER #" password = "#PASS #" >

SELECT *.

Oeitem I have

LEFT JOIN oevendor v

ON v.vendorid = i.vendorid

LEFT JOIN OEVendorCoupons c

ON i.itempromocode = c.couponid

WHERE itemid = "#i #

< / cfquery >

< td width = "210" valign = "top" >

< table width = "200" border = "0" align = "center" cellpadding = "2" cellspacing = "0" >

< class tr 'f10' = >

< td align = "center" > < img src = "x.png" height = "8" / > < table > "

< /tr >

< CFIF Len (si.itemshortdesc) WG 18 >

< CFSET sdl = 18 - ListLen (si.itemshortdesc) >

< CFSET sd = ' #Left (si.itemshortdesc, sdl) #...» ">

< CFELSE >

< CFSET si.itemshortdesc = sd >

< / CFIF >

< b >

#si.vendorcompany # < td > < table >

< /tr >

< /table >

< table >

< / CFIF >

< / CFLOOP >

< /tr >

< /table >

< / CFOUTPUT >

Thank you

Chuck

== cfSearching ==-a writes:

Update: I just noticed your date filter, we passed in our pseudocode. They should be added to your subquery so it pulls the correct records. Otherwise it will just get three random records, regardless of the price and date.  Try adding filters back and see if that fixes the problem.  (Test the subquery separately first of course.)

....

WHERE i.itemid IN ((in English only)

SELECT TOP 3 itemid from oeitem WHERE itemsaleprice > 0 AND coupon_exp > = GetDate() ORDER OF NEWID()

)

It's funny you should say that. I was wondering if the best solution would be to get exactly these itemIDs that match the query. In other words, in order to reproduce the original query in the where clause, as follows

SELECT *.
Oeitem I have
LEFT JOIN OEVendorCoupons c
ON i.itempromocode = c.couponid
WHERE i.itemsaleprice > 0 AND c.coupon_exp > = GetDate()
AND i.itemid in

(SELECT j.itemid
Of oeitem j
LEFT JOIN OEVendorCoupons k
ON j.itempromocode = k.couponid
WHERE j.itemsaleprice > 0 AND k.coupon_exp > = GetDate()
ORDER BY NEWID())

Tags: ColdFusion

Similar Questions

  • unique random numbers

    How to get the unique random numbers each time in AS3

    I searched but not able to get satisfactory results

    use:

    var tempArray:Array =]

    for (var i: uint = 0; i<>

    tempArray.push (i);

    }

    Shuffle (tempArray);

    You can now browse tempArray for random integers from 0 to moveXml.movie.length () - 1

    function shuffle(a:Array) {}
    var p:int;
    var t: *;
    var ivar:int;
    for (ivar =. Length-1; Ivar > = 0; Ivar-) {}
    p = Math.Floor ((Ivar+1) * Math.Random ());
    t = a [ivar];
    a [ivar] = a [p];
    a [p] = t;
    }
    }

  • How can I generate multiple unique random numbers?

    Hello

    I am trying to generate multiple random numbers between a given set of numbers (1-52) and do not have the same number generated twice within this group. I can compare the numbers of last and next with the function to compare, but how would I go about comparison of all numbers generated without using a huge list of shift registers...

    Any help/ideas are welcome and appreciated.

    Jason

    Solved my problem. IM passing the random number through a registry to offset to each case and build a table every time. I then searches the table for the new random number. If the number is not found I get a value of-1, another thing is an index value of 0-everything. If a comparator greater than (-1) indicates that the same number is in the table and then I can raise this matter until the same number is not found.

    Kind regards

    Jason

  • Effective unique random numbers

    Hi all

    I'm looking for assign a random number from 1-20 from 20 different variables. I need each variable to have a unique number. Later, I put variables into a list, and because of the randomly generated number that the list is in a different order each time the program is run. What I have works, but I think it's bulky. I was wondering if there was a better way to do this. Thank you:

    Here is an excerpt of how I am the procedure, which shows the process for the first 10 variables, but the last 10 follow the same pattern:

    N1 = Math.floor (Math.random () * 20) + 1;

    questionRandomizer1 ();

    questionRandomizer2 ();

    questionRandomizer3 ();

    questionRandomizer4 ();

    questionRandomizer5 ();

    questionRandomizer6 ();

    questionRandomizer7 ();

    questionRandomizer8 ();

    questionRandomizer9 ();

    questionRandomizer10 ();

    questionRandomizer11 ();

    questionRandomizer12 ();

    questionRandomizer13 ();

    questionRandomizer14 ();

    questionRandomizer15 ();

    questionRandomizer16 ();

    questionRandomizer17 ();

    questionRandomizer18 ();

    questionRandomizer19 ();

    questionRandomizer20 ();

    function questionRandomizer1)

    {

    QuestionOrder [1] = n1;

    }

    function questionRandomizer2)

    {

    QuestionOrder [2] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [2] == QuestionOrder [1]) {questionRandomizer2 ()}

    }

    I'm generates a random number to QuestionOrder [2], then check if it is the same number as QuestionOrder [1], and in this case, I'm re - run the function to generate a random number again, until he gets a unique number. This continues for each next function.

    function questionRandomizer3)

    {

    QuestionOrder [3] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [3] == QuestionOrder [1] |) QuestionOrder [3] == QuestionOrder [2]) {questionRandomizer3 ()}

    }

    function questionRandomizer4)

    {

    QuestionOrder [4] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [4] == QuestionOrder [1] |) QuestionOrder [4] == QuestionOrder [2] | QuestionOrder [4] == QuestionOrder [3]) {questionRandomizer4 ()}

    }

    function questionRandomizer5)

    {

    QuestionOrder [5] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [5] == QuestionOrder [1] |) QuestionOrder [5] == QuestionOrder [2] | QuestionOrder [5] == QuestionOrder [3] | QuestionOrder [5] == QuestionOrder [4]) {questionRandomizer5 ()}

    }

    function questionRandomizer6)

    {

    QuestionOrder [6] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [6] == QuestionOrder [1] |) QuestionOrder [6] == QuestionOrder [2] | QuestionOrder [6] == QuestionOrder [3] | QuestionOrder [6] == QuestionOrder [4]

    || QuestionOrder [6] == QuestionOrder [5]) {questionRandomizer6 ()}

    }

    function questionRandomizer7)

    {

    QuestionOrder [7] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [7] == QuestionOrder [1] |) QuestionOrder [7] == QuestionOrder [2] | QuestionOrder [7] == QuestionOrder [3] | QuestionOrder [7] == QuestionOrder [4]

    || QuestionOrder [7] == QuestionOrder [5] | QuestionOrder [7] == QuestionOrder [6]) {questionRandomizer7 ()}

    }

    function questionRandomizer8)

    {

    QuestionOrder [8] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [8] == QuestionOrder [1] |) QuestionOrder [8] == QuestionOrder [2] | QuestionOrder [8] == QuestionOrder [3] | QuestionOrder [8] == QuestionOrder [4]

    || QuestionOrder [8] == QuestionOrder [5] | QuestionOrder [8] == QuestionOrder [6] | QuestionOrder [8] == QuestionOrder [7]) {questionRandomizer8 ()}

    }

    function questionRandomizer9)

    {

    QuestionOrder [9] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [9] == QuestionOrder [1] |) QuestionOrder [9] == QuestionOrder [2] | QuestionOrder [9] == QuestionOrder [3] | QuestionOrder [9] == QuestionOrder [4]

    || QuestionOrder [9] == QuestionOrder [5] | QuestionOrder [9] == QuestionOrder [6] | QuestionOrder [9] == QuestionOrder [7] | QuestionOrder [9] == QuestionOrder [8]) {questionRandomizer9 ()}

    }

    function questionRandomizer10)

    {

    QuestionOrder [10] = Math.floor (Math.random () * 20) + 1;

    If (QuestionOrder [10] == QuestionOrder [1] |) QuestionOrder [10] == QuestionOrder [2] | QuestionOrder [10] == QuestionOrder [3] | QuestionOrder [10] == QuestionOrder [4]

    || QuestionOrder [10] == QuestionOrder [5] | QuestionOrder [10] == QuestionOrder [6] | QuestionOrder [10] == QuestionOrder [7] | QuestionOrder [10] == QuestionOrder [8]

    || QuestionOrder [10] == QuestionOrder [9]) {questionRandomizer10 ()}

    }

    as you can see the checklist gets progressively longer. I feel that I do a ton of If, Then, Else statements, and usually when this happens I will use a system of records. IM wondering if there is a better way to do it, in the same way a case statement is more effective than a bunch of If, then, Else statements.

    Thank you.

    Just create an array with 20 values in either randomly select them (and then splice), or probably even better, just mix table and retrieve ordered items randomly in the order they come.

    Here is a function for the table of brewing in AS3:

    function shuffle(a:Array) {}
    var p:int;
    var t: *;
    var ivar:int;
    for (ivar =. Length-1; Ivar > = 0; Ivar-) {}
    p = Math.Floor ((Ivar+1) * Math.Random ());
    t = a [ivar];
    a [ivar] = a [p];
    a [p] = t;
    }
    }

    Use:
    Shuffle (anArray);

  • Unique random number

    Hello

    Oracle 11.2.0.1

    Windows

    I have a table something like this:

    create table (ID, number of rnd) rnd;

    I need a process something like this:

    exec genrnd (200,300);

    After executing the procedure, there will be 100 lines in the table of rnd and there will be unique random numbers in the column of rnd.  OK, suppose if I run once again something like this:

    exec genrnd (173,514);

    therefore table will be truncated and (514-173) + 1 = 342 lines will be rnd table and new unique random numbers will be in the column of rnd.

    Column ID is just at the end of the order of.  There will be 1,2,3... 100 or 1,2,3,4... numbers 342.

    I get random number in this way:

    Select trunc (dbms_random.value (200,300)) rnd twice;

    Select trunc (dbms_random.value (173,514)) rnd twice;

    but number must be unique within the given range, so I don't get how this is possible, please help me.

    Thank you.

    Insert into rnd

    Select rownum, one of

    (

    Select * from

    (

    Select rownum + start_value - 1A

    Double connect rownum<= end_value="" -="" start_value="" +="">

    )

    order of dbms_random.value

    );

    This query has 4 parts

    (1) generation X Y numbers in order

    (2) randomized data generated

    (3) assign a rownum for each row of data

    (4) Insert the result in the table

    So he adapts to the requirements of the opus and don't know how

    > Order by on the insert does nothing

    available in photo with this code.

  • I want just a bunch of random numbers (about 120 of them) from the list, select 'all' and get the computer to rearrange in CNC

    I try both notepad and wordpad. I'm in Winows XP 2003.  I want just a bunch of random numbers (about 120 of them) from the list, select "all" and reorganize in order digital computer. I can't understand how I've done it before. Google says select the balls feature, but all that is put a point in front of a certain number. There is no arrow down to select. No A - Z. No 1,2,3. what Miss me? is the Notepad or wordpad not the place to do that?

    Use Excel.

  • 3 display unique and random numbers on 6

    The following program gives me the results in the Panel out because I used the trace.  The problem is I want these numbers when I click on a button.  The numbers do not again when the button is clicked, for example not "3, 3, 2" but "3, 2, 5".»  Here is the program that selects random numbers (which, incidentally, are not mine): (I'll give gladly $15 to the animal charity of your choice for the one who can give me a hand with this problem, thank you!)

    var mynumbers:Array is ['1', '2', '3', '4', '5', '6'];.

    var mystuff:Array = [];

    var randomCount:Number = 3;

    var r: Number;

    for (var i = 0; i < randomCount; i ++)
    {

    r = Math.floor (Math.random () * mynumbers.length);

    Mystuff [mystuff. Length] = mynumbers.splice (r, 1);

    }

    trace (mystuff);

    and here is my button code

    generate_btn.addEventListener (MouseEvent.CLICK, coco);

    //3.;
    function coco(event:MouseEvent):void
    {
    }

    Oops, sorry.

    use:

    var mynumbers:Array is ['1', '2', '3', '4', '5', '6'];.

    var randomCount:Number = 3;

    function shuffle(a:Array) {}
    var p:int;
    var t: *;
    var ivar:int;
    for (ivar =. Length-1; Ivar > = 0; Ivar-) {}
    p = Math.Floor ((Ivar+1) * Math.Random ());
    t = a [ivar];
    a [ivar] = a [p];
    a [p] = t;
    }
    }

    and here is my button code

    generate_btn.addEventListener (MouseEvent.CLICK, coco);

    //3.;
    function coco(event:MouseEvent):void
    {

    shuffle(mynumbers);
    

    for (var i: int = 0; i<>

    {if(!this["tf_"+i])}

    This ["tf_" + i] = new TextField();

    }

    This ["tf_" + i] .text = mynumbers [i];

    addChild (this ["tf_" + i]);

    This ["tf_" + i] there = i * 40;

    }

    }

    Thanks for the giveaway.  That's very generous of you.

  • Choosing random numbers and unique serveral

    Hello

    I would select 9 random numbers of a series we'll say 1-20. They cannot repeat and each must be recorded in a different variable P1 - P9.

    Any ideas?
    Thank you

    Make a table of numbers 1 to 20.
    Mix the table.
    Remove as many numbers as you need.

    Search the forums for shuffle learn how to mix the table.

  • 5 digit unique random number based on the year

    Hello
    I need 5 digit unique random number based on the year. If the new year then duplicate random number allowed.
    Thank you

    If only 5-digit numbers are needed you can extract some transformation injective (http://en.wikipedia.org/wiki/Injective_function) and you could easily check for uniqueness:
    substr (to_char (ln (sequence_value)), 10, 5)
    substr (to_char (exp(1 / sequence_value)), 20, 5)
    ...

    Concerning

    Etbin

  • Another problem of random numbers

    I say 'Other' because there is an existing thread of random numbers on the first page of this forum, but it is unresponsive to my problem.

    I'm generates names for an array of objects. I have an array of words (strings) to the choice. I use Math.random () * 151 to select indices array of two words. I want to check that the two chosen integers are not the same before assigning the name to the object. Then, I want to check to make sure that none of the two objects have the same name. Simple, right? Here is the code:
        for(int i=0;i<numStars;i++){
            ok=true;
            do{
            int part1=(int)(Math.random()*151);//first index
            int part2=(int)(Math.random()*151);//second index
            if (part1==part2){//make sure they're not the same number
                ok=false;
                System.out.println(part1+" "+part2);
            }else{
                stars.name=new String(starName[part1]+" "+starName[part2]);
    for(int j=0;j<i;j++){
    if(stars[i].name.equals(stars[j].name)) {//make sure no two stars have the same name
    ok=false;
    System.out.println(stars[i].name+" "+stars[j].name);
    }
    }
    }
    }while( ok==false);
    )//end of for loop


    The problem is, if integers part1 and part2 are equal in one pass through the do loop, they remain identical in each subsequent pass. The number doesn't stay the same, but whatever part1 is, part2 is always the same thing. This is only true after matching up one time. It's possible for it to run without ever hitting a duplicate.

    I changed it to ensure that part1 and part2 weren't covering the same range of numbers, and the same error started happening in the lower for loop that compares one object's name to another. This second-level error doesn't occur when part1 and part2 both cover the full range.

    Edited by: 800048 on Oct 11, 2010 3:17 PM

    Edited by: 800048 on Oct 11, 2010 3:25 PM

    800048 wrote:
    The problem is that if whole part1 and part2 are equal in one pass through the do loop, they remain identical in each subsequent pass.

    I don't think it's possible with the code you have posted.

    What will happen, however, given that you set never ok = true once part1 and part2 become unequal, is if they are equal in the first pass through the do / while loop, you will never get out of this loop.

    It is also unnecessary and bad form to use == or! = with the value true or false

    // bad
    x == true
    x != false
    
    // good
    x
    
    // bad
    x != true
    x == false
    
    // good
    !x
    
  • Generating random numbers with format 18XX88YYYYY

    I've been tryig to generate random numbers with
    format 18xx88yyyyy
    here
    18, followed by two random number then 88 followed by five random number. Here 18 and 88 are set to place one, two, five and sixth position.

    but I am unable to get the logic.

    So please help


    Thanx

    Achyot

    Try this:

    SELECT    '18'
           || ROUND (DBMS_RANDOM.VALUE (1, 100))
           || '88'
           || ROUND (DBMS_RANDOM.VALUE (1, 100000))
      FROM DUAL
    

    - - - - - - - - - - - - - - - - - - - - -
    Kamran Agayev a. (10g OCP)
    http://kamranagayev.WordPress.com

  • Random numbers appearing in messages

    I am running Firefox 4.0.1 on 64-bit Windows 7 and every time that I post on a forum or send a Gmail account, it adds random numbers at the end of my entry. I tried using Chrome and it doesn't do this, what's the problem?

    You paste content or add a signature?

    Start Firefox in Firefox to solve the issues in Safe Mode to check if one of the extensions of the origin of the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > appearance/themes).

  • The value of the seed of the generator of random numbers in Matlab node

    Hi all

    I need to use random variables inside a Matlab Labview Commsuite node. I generate with the randn() function. Is it possible to put the seed of the random number generator such as the random numbers are reproducible?

    Hi Steve,.

    You can use randn ('seed', seeds) to initialize the random number generator. The seed must be a positive integer value. For example, randn ('seed', 3). In addition, this seed is global within the entire application. This means that if the randn() function is called MathScript multi-node that could run in parallel, random results are not guaranteed to be the same after you change any part of your code. But you should always get the same results for repeated passages of the same code.

  • Medium-sized random numbers

    So what I need to do, is to generate a series of random numbers. I've got that.

    However, the tricky part is that I need to average the random number and the previous 3 numbers random and exit on a waveform graph.

    Any help or pointers would be great. Thank you

    Simple as pie. He called a VI means Pt by PT. set your buffer to 4 and it will be an average intensity absorbed with the last 3.

  • How to generate random numbers from 1 to 5

    How to generate random numbers from 1 to 5

    -1110340081

    Thank you I ended up

Maybe you are looking for