• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

making sure a variable doesn't equal another variable

Explorer ,
Jul 22, 2018 Jul 22, 2018

Copy link to clipboard

Copied

Hi,

I want to generate a random number but make sure that the number doesn't equal another number in a recordeet.  I am using this but I don't think it's right.

<cfoutput query="rsHistory">

<cfset historyid = RandRange(1,1999999999)>

<cfif #historyid# EQ #rsHistory.history_id#>

<cfset historyid = RandRange(1,1999999999)>

</cfif>

</cfoutput>

Is there a better way of doing this?

Thanks,

MIke

EDIT: I'm using coldfusion 8

Views

450

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2018 Jul 22, 2018

Copy link to clipboard

Copied

I do this:

<cfscript>

    IsUnique_Voucher_Number=False;

</cfscript>

<cfloop condition="IsUnique_Voucher_Number EQ False">

    <cfscript>
   
        Unique_Voucher_Number=RandRange(0, 99999999, "SHA1PRNG");
   
    </cfscript>

    <cfquery name="Query_Check_Voucher_Number" datasource="#Request.JustInLime_DataSourceName#" result="Query_Issued_List_Result">
        SELECT
            Lower(JustInLime_VoucherNumber) AS JustInLime_VoucherNumber
        FROM
            #Request.JustInLime_DatabasePrefix#_Issued_JoinOnline
        WHERE
            JustInLime_VoucherNumber=<cfqueryparam cfsqltype="cf_sql_varchar" value="#Unique_Voucher_Number#" />
    </cfquery>

    <cfif Query_Check_Voucher_Number.RecordCount>

    <cfelse>

        <cfscript>

            IsUnique_Voucher_Number=True;
            Form.JustInLime_VoucherNumber=NumberFormat(Unique_Voucher_Number, "00000000");
       
        </cfscript>
   
    </cfif>
   
</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2018 Jul 22, 2018

Copy link to clipboard

Copied

Can you break that down a little for me?  I just want to generate a random number that does not equal a variable from a recordset. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2018 Jul 22, 2018

Copy link to clipboard

Copied

ok so i'm trying this but I got an error saying\

ROOT CAUSE:

java.lang.OutOfMemoryError: Java heap space

<cfloop condition = "isUnique EQ True">

<cfset historyid = RandRange(1,1000000000)>

<cfoutput query="rsHistory">

<cfif '#historyid#' EQ '#rsHistory.history_id#'>

<cfset isunique = False>

<cfelse>

<cfset isUnique = True>

</cfif>

</cfoutput>

</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 22, 2018 Jul 22, 2018

Copy link to clipboard

Copied

I think your original code is OK, with just the addition of the algorithm:

<cfoutput query="rsHistory">

<cfset historyid = randRange(1,1999999999, "SHA1PRNG")>

<cfif historyid EQ rsHistory.history_id>

<cfset historyid = randRange(1,1999999999, "SHA1PRNG")>

</cfif>

</cfoutput>

But remember you are looping through a query. If you want a random ID for each row, then do

<cfset historyid = arrayNew(1)>

<cfoutput query="rsHistory">

<cfset historyid[currentRow] = randRange(1,1999999999, "SHA1PRNG")>

<cfif historyid[currentRow] EQ rsHistory.history_id[currentRow]>

<cfset historyid[currentRow] = randRange(1,1999999999, "SHA1PRNG")>

</cfif>

</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

What does the [currentrow] part mean?  I only want to generate one random number that doesn't equal one of the rsHistory.history_id values.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

LATEST

CurrentRow is a variable that ColdFusion automatically generates within cfoutput for the query. The name literally says what it means: currentRow is 1 for the 1st row, 2 for the 2nd row, 3 for the 3rd row and so on.

So, for example, when you write

historyId[currentRow]=rsHistory.history_id[currentRow]

you mean

historyId[1]=rsHistory.history_id[1]

historyId[2]=rsHistory.history_id[2]

historyId[3]=rsHistory.history_id[3]

etc.

If you wish to get just the first satisfactory number, then break out of the loop when you find it:

<cfset historyid = randRange(1,1999999999, "SHA1PRNG")>

<cfloop condition="true">

<cfif historyid EQ rsHistory.history_id>

    <cfset historyid = randRange(1,1999999999, "SHA1PRNG")>

<cfelse>

    <cfbreak>

</cfif>

</cfloop>

This improves the efficiency of your code

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation