The code above produces one of 1, 2, 3, 4, or 5 randomly.Code:<cfoutput> #RandRange(1, 5)# </cfoutput>
I like to produce one of 1 or 5 randomly.
Is that a way to get one of 1 or 5 randomly?
| SitePoint Sponsor |





The code above produces one of 1, 2, 3, 4, or 5 randomly.Code:<cfoutput> #RandRange(1, 5)# </cfoutput>
I like to produce one of 1 or 5 randomly.
Is that a way to get one of 1 or 5 randomly?


#RandRange(0,1)# * 4 + 1
![]()





I have data in myTable like the above.Code:data in myTable (id) continent city (1) 2 Rome (2) 1 Peking (3) 1 Seoul (4) 2 Paris (5) 1 Tokyo (6) 3 New York (7) 2 Berlin (8) 3 Montreal
The following code1 produce the following result.
The following would-be code doesn't work correctlyb but it will show what I want.Code:code1 select city from myTable where continent=1 result1 Peking Seoul Tokyo
Can I get my target result above with your help?Code:would-be code select city(call 1 city randomly) from myTable where continent=1 target result Peking, Seoul, or Tokyo(randomly)
Last edited by dotJoon; Nov 4, 2008 at 04:04.





Thank you, the code works fine.Code:#RandRange(0,1)*4+1#





The code above produces 1 random number from 1 to 10.Code:<cfoutput> #RandRange(1, 10)# </cfoutput>
I like to produce 1 random number from 1 to 10 except 7 and 9.
( I like to produce 1,2,3,4,5,6,8, or 10.)


<cfset joon = 7>
<cfloop condition = "joon EQ 7 OR joon EQ 9">
<cfset joon = RandRange(1,10)>
</cfloop>
<cfoutput>
#joon#
</cfoutput>





Thank you. it works fine.
Now I have another question related this issue.
It's start from randon issue. but I like to ask you about how to put data value into list value.
The code above produce the data value randomly.Code:<cfquery datasource='db2' name='test'> select id from myTable2 order by id </cfquery> <cfoutput> <cfloop query='test'> (#id#)<br> </cfloop> <cfloop query='test'> <cfset 'id#currentRow#'=test.id> </cfloop> <cfset myList='#variables.id1#, #variables.id2#, #variables.id3#,#variables.id4#'> <cfset randListElement = ListGetAt(mylist,RandRange(1,ListLen(myList))) > <cfoutput> #variables.randListElement# </cfoutput>
As I don't know much about list, I guess there might be easier way than the code above.
The code above produece a random value from the 4 variables.
If I need 40 or 400 data, do I have to write it like the below?
Code:<cfset myList='#variables.id1#, #variables.id2#, #variables.id3#,#variables.id4#, ... ... #variables.id399#, #variables.id400#'>





I got it by myself.
ValueList(test.id) is the answer!!!





The variable getOne_randomly produeces one number among numbers in myList.Code:<cfset myList='1,3,4,7,9,12,15,18'> #myList# <cfset getOne_randomly = ListGetAt(mylist,RandRange(1,ListLen(myList))) >
I like to produce 3 numbers exclusively among numbers in myList.


Last edited by r937; Nov 4, 2008 at 06:35.



Assuming your list always has at least three elements, loop from 1 to 3. Within each loop, save the random element then remove it from the array to ensure it is not selected again. Add validation if your list may have less than 3 elements.
Code:<cfset threeRandom = ""> <cfloop from="1" to="3" index="p"> <cfset pos = RandRange(1,ListLen(myList))> <!--- save the random element ---> <cfset threeRandom = listAppend(threeRandom, listGetAt(myList, pos))> <!--- remove the element ---> <cfset mylist = ListDeleteAt(mylist, pos) > </cfloop> <cfoutput>#threeRandom#</cfoutput>



Doesn't your database have functions for ordering records randomly and limiting the number of records returned?
http://www.petefreitag.com/item/466.cfm

Code:select city from myTable where continent=1 order by rand() limit 1
Check out our new Industry News forum!
Keep up-to-date with the latest SP news in the Community Crier
I edit the SitePoint Podcast
Bookmarks