First I run a simple query to get the max ID from the table, which
returns a number 10284. To which I would like to add a randomly generated number. So I get a number like '2' and it adds it and I get 10287 - which is INCORRECT! I was expecting 10286. So what am I doing wrong? I've run this dozens of times and I can't find a pattern to it...the random number is generating nicely but the results of the two values are rarely what I expect. Thanks for any help!

<cfquery name="Find_Max" datasource="#application.dbfile#">
   SELECT max(MainID) as maxID
   FROM Main
</cfquery>

Returns a number from the table like 10284.

<cfset total =  val(#Find_Max.MaxID#) + val(#RandRange(1,10)#)>
 
<cfoutput>val(#RandRange(1,10)#)</cfoutput><br />
<CFOUTPUT>#Find_Max.MaxID# + random number = #total#</CFOUTPUT>

Recommended Answers

All 4 Replies

I would hope that

..the random number is generating nicely but the results of the two values are rarely what I expect.

You are setting two different random numbers. Try setting it as a variable:

<cfquery name="Find_Max" datasource="#application.dbfile#">
   SELECT max(MainID) as maxID
   FROM Main
</cfquery>

<cfset intRand = val(#RandRange(1,10)#)>
<cfset total =  val(#Find_Max.MaxID#) + intRand>

<CFOUTPUT>#Find_Max.MaxID# + #intRand# = #total#</CFOUTPUT>
Member Avatar for vsejpal

Try this..

<cfquery name="Find_Max" datasource="#application.dbfile#">
   SELECT max(MainID) as maxID
   FROM Main
</cfquery>

Returns a number from the table like 10284.

<cfset myRandNum = val(#RandRange(1,10)#)>
<cfset total =  val(#Find_Max.MaxID#) + myRandNum  >

<cfoutput> #myRandNum#  </cfoutput><br />
<CFOUTPUT>#Find_Max.MaxID# + #myRandNum# = #total#</CFOUTPUT>

Basically every time you have call the RandRange function, it will generate a new random number for you. This is not what you want.

The solution is to call the RandRange function once and store its returned output to a local variable. Use this local variable for outputting the values.

Let me know if that was helpful..:)

<cfset total =  val(#Find_Max.MaxID#) + val(#RandRange(1,10)#)>
<cfoutput>val(#RandRange(1,10)#)</cfoutput><br />
<CFOUTPUT>#Find_Max.MaxID# + random number = #total#</CFOUTPUT>

A few tips on more elegant code
1) RandRange _always_ returns a number, so using VAL() is unnecessary.
2) The pound # signs aren't needed except in the CFOUTPUT
3) Rather than using multiple CFOUTPUT tags, just use one set and put all of the output code inside it

<cfset theRandNum = RandRange(1,10)>
<cfset total =  val(Find_Max.MaxID) + theRandNum >
<CFOUTPUT>
  random number = #theRandNum#<br>
  #Find_Max.MaxID# + random number = #total#
</CFOUTPUT>

First I run a simple query to get the max ID from the table, which
returns a number 10284. To which I would like to add a randomly generated number. So I get a number like '2' and it adds it and I get 10287 - which is INCORRECT! I was expecting 10286. So what am I doing wrong? I've run this dozens of times and I can't find a pattern to it...the random number is generating nicely but the results of the two values are rarely what I expect. Thanks for any help!

<cfquery name="Find_Max" datasource="#application.dbfile#">
   SELECT max(MainID) as maxID
   FROM Main
</cfquery>

Returns a number from the table like 10284.

<cfset total =  val(#Find_Max.MaxID#) + val(#RandRange(1,10)#)>
 
<cfoutput>val(#RandRange(1,10)#)</cfoutput><br />
<CFOUTPUT>#Find_Max.MaxID# + random number = #total#</CFOUTPUT>

I guess the reason why the total value is not the value that you did expect is that you did call <cfoutput>val(#RandRange(1,10)#)</cfoutput><br />
which generates another value which is not the same as the
value returned when you did called
val(#RandRange(1,10)#)
in getting the total.

<cfset total =  val(#Find_Max.MaxID#) + val(#RandRange(1,10)#)>

you can try this code....

<cfquery name="Find_Max" datasource="#application.dbfile#">
   SELECT max(MainID) as maxID
   FROM Main
</cfquery>

<cfset random_number = RandRange(1,10)> 
<cfset total =  val(Find_Max.MaxID) + val(random_number) >
 
<cfoutput>
      random number: #random_number#<br/>
      100 + #rand# = #total#
</cfoutput>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.