<html>
<head>
     <title>Generate 5 Random Numbers</title>
</head>

<body>
<CFQUERY NAME="test" DATASOURCE="Foo">
    SELECT manager_id
    FROM manager_data
</CFQUERY>

<cfset templist = "">
<cfset tempnum = "">
<cfset counter = 0>
<!--- Beginning: Sets valid values for random number generation --->
<cfset checklist = valuelist(test.manager_id,",")>
<!--- End: Sets valid values for random number generation --->

     <!--- Set condition to: counter lt (however many random numbers you
want to generate) - in this case 5. --->
     <cfloop condition="counter lt 5">
          <cfset tempnum = randrange(1,  #test.recordcount#)>
          <!-- Beginning: Ensures the number is not already in the randomly
generated list and that the
               randomly generated number has a match in the source table
-->
          <cfif listfind(templist,"#tempnum#", ",") eq 0 and
listfind(checklist,"#tempnum#",",") gt 0>
          <!-- End: Ensures the number is not already in the randomly
generated list and that the
               randomly generated number has a match in the source table
-->
               <cfif listlen(templist, ",") lt 5>
                    <CFSET templist = ListAppend(templist, "#tempnum#",
",")>
                    <cfset counter = counter + 1>
               </cfif>
          </cfif>

          <!--- Beginning of Output Inside Loop for Correct Random Number
Generation --->
          TempNumber: <cfoutput>#tempnum#</cfoutput><br>
          TempList:<cfoutput>#templist#</cfoutput><br>
          TempListLength:<cfoutput>#listlen(templist, ",")#</cfoutput>
          <!--- End of Output Inside Loop for Correct Random Number
Generation --->

     </cfloop>

<!--Select records created using the randomly generated list -->
<CFQUERY NAME="test1" DATASOURCE="Foo">
    SELECT manager_id, manager_name
    FROM manager_data
     WHERE manager_id in (#listqualify(templist,"'",",","ALL")#)
</CFQUERY>
<br><br>
<!--- Beginning of Output to check for Correct Output based on randomly
generated List --->
<cfoutput query="test1">
Manager_ID: #test1.manager_id#<br>
Manager_Name:#test1.manager_name#<br>
</cfoutput>
<!--- End of Output to check for Correct Output based on randomly generated
List --->

</body>
</html>


Only records that do not currently exist in the randomly generated list and
that have a match in the source table get appended
to the randomly generated list, and outputed in the Sample Output below.
Thought I would throw that comment in because as
you can see the first 2 randomly selected number did not have matches in
the source table and therefore did not get appended to the list.
You can also see that  the number 1074 was selected twice and not appended
ot the list a second time.

Example Output:

TempNumber: 930
TempList:
TempListLength:0 TempNumber: 2178
TempList:
TempListLength:0 TempNumber: 1715
TempList:1715
TempListLength:1 TempNumber: 1074
TempList:1715,1074
TempListLength:2 TempNumber: 172
TempList:1715,1074,172
TempListLength:3 TempNumber: 1074
TempList:1715,1074,172
TempListLength:3 TempNumber: 1177
TempList:1715,1074,172,1177
TempListLength:4 TempNumber: 731
TempList:1715,1074,172,1177,731
TempListLength:5

Manager_ID: 1177
Manager_Name:Mouse, Mickey
Manager_ID: 1715
Manager_Name:Three, Quake
Manager_ID: 731
Manager_Name:TBD
Manager_ID: 172
Manager_Name:Fudd, Elmer
Manager_ID: 1074
Manager_Name:Man, Super

Comments: If you want a random number generator NOT based on valid values
from a table then just remove the code below
     from the code above.

<cfset checklist = valuelist(test.manager_id,",")>

and

delete everything after the "and" in this statement

<cfif listfind(templist,"#tempnum#", ",") eq 0 and listfind(checklist,"
#tempnum#",",") gt 0>

will become <cfif listfind(templist,"#tempnum#", ",") eq 0>


Enjoy,
Casey Cook




"Dan Slater" <[EMAIL PROTECTED]> on 07/28/2001 01:19:30 PM

To:   Casey C Cook/TMG/CSC@CSC
cc:
Subject:  RE: $*&^% Random Numbers!!!


not yet, working on other stuff now

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 27, 2001 2:15 PM
To: [EMAIL PROTECTED]
Subject: Re: $*&^% Random Numbers!!!


Did you ever solve this?

CC




"Dan Slater" <[EMAIL PROTECTED]>@dfwcfug.org on 07/20/2001 03:43:54
PM

Please respond to [EMAIL PROTECTED]

Sent by:  [EMAIL PROTECTED]


To:   <[EMAIL PROTECTED]>
cc:
Subject:  $*&^% Random Numbers!!!


Hi all,

I've tried and tried, and tried again to solve this one on my own with no
success.  I'm sure it's pretty ez, but since i'm a "coder" and not a
"programmer" this is just beyond my abilities i think.

Designing a shopping page that should randomly pull up to 5 records
(stores).  The random number generated should NOT be a duplicate of it self
at any time through the loop...  That last part is the part I can't seem to
do...

Sigh.

Here's the code i've got right now (try not to laugh):

<CFSET temp = ValueList(getStores.pkStoreID)>
<CFSET tempnum = #ListLen(temp)#>
<CFOUTPUT>
<table border="0" align="center" width="100%">
  <cfset NewNum = 0>
  <cfloop query="getStores" startrow="1" endrow="5">
    <CFSET randtemp = #RandRange(1,tempnum)#>
    <CFSET #RANDNUMBER# = #ListGetAt(temp, randtemp)#>
    <cfloop condition="#NewNum# EQ #RANDNUMBER#">
      <CFSET randtemp = #RandRange(1,tempnum)#>
      <CFSET #RANDNUMBER# = #ListGetAt(temp, randtemp)#>
    </cfloop>
    <cfquery name="qryStores" datasource="#DSN3#">
      SELECT     tblStores.pkStoreID, tblStores.StoreName,
tblStores.StoreDesc, tblStores.StoreLogo,
                 tblStores.URL, tblStores.TierID, tblSubCategories.CatID,
tblSubCategories.pkSubCatID
      FROM       tblStores
      INNER JOIN tblStoresSubCatsLink ON tblStores.pkStoreID =
tblStoresSubCatsLink.StoreID
      INNER JOIN tblSubCategories ON tblStoresSubCatsLink.SubCatID =
tblSubCategories.pkSubCatID
      WHERE      tblStores.pkStoreID = #RANDNUMBER#
    </cfquery>
    <tr>
      <td valign="top"><a
href="get_store.cfm?Store=#qryStores.pkStoreID)"><img
src="images/#qryStores.StoreLogo#.jpg" height="125" width="125"></a></td>
      <td valign="top">#qryStores.StoreDesc#...<a
href="store.cfm?StoreID=#qryStores.pkStoreID#" class="ShopNow">SHOP
NOW</a></td>
    </tr>
    <cfset NewNum = #RANDNUMBER#>
  </cfloop>
</table>
</CFOUTPUT>

I tried to use #NewNum# to check to see if a dupe was being made,
unfortunately, that method only works when you work with just 2 records,
any
more than that and you will sometimes get a dupe.

Tried to store (and grow) a list of #RandNumber#'s generated, and compare
that to the currently generated #RandNumber# - if dupe found, keep creating
#RandNumber# until no duplicate...  Didn't work, resulted in an endless
loop
i think, or a very slow one...

This would be SO much easier if SQL Server or Cold Fusion could randomly
select 5 records from a query with a simple command...sigh....

Thanks in advance,

Dan Slater


-------------------------------------------------------------------------
This email server is running an evaluation copy of the MailShield anti-
spam software. Please contact your email administrator if you have any
questions about this message. MailShield product info: www.mailshield.com

-----------------------------------------------
To post, send email to [EMAIL PROTECTED]
To subscribe / unsubscribe: http://www.dfwcfug.org







~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to