SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    joon's threads (merged) about random numbers

    Code:
    <cfoutput>
    #RandRange(1, 5)#
    </cfoutput>
    The code above produces one of 1, 2, 3, 4, or 5 randomly.

    I like to produce one of 1 or 5 randomly.

    Is that a way to get one of 1 or 5 randomly?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    #RandRange(0,1)# * 4 + 1

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    randomly call 1 city

    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
    I have data in myTable like the above.

    The following code1 produce the following result.

    Code:
    code1
    
    select city
    from myTable
    where continent=1
    
    result1
    
    Peking
    Seoul
    Tokyo
    The following would-be code doesn't work correctlyb but it will show what I want.

    Code:
    would-be code
    
    select city(call 1 city randomly)
    from myTable
    where continent=1
    
    
    target result
    
    Peking, Seoul, or Tokyo(randomly)
    Can I get my target result above with your help?
    Last edited by dotJoon; Nov 4, 2008 at 04:04.

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    #RandRange(0,1)*4+1#
    Thank you, the code works fine.

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    1 randdom number from to 10 except 7 and 9

    Code:
    <cfoutput>
    #RandRange(1, 10)#
    </cfoutput>
    The code above produces 1 random number from 1 to 10.

    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.)

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    <cfset joon = 7>
    <cfloop condition = "joon EQ 7 OR joon EQ 9">
    <cfset joon = RandRange(1,10)>
    </cfloop>
    <cfoutput>
    #joon#
    </cfoutput>
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    <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.

    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>
    The code above produce the data value randomly.
    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#'>

  8. #8
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I got it by myself.

    ValueList(test.id) is the answer!!!

  9. #9
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    getThree_randomly

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

    I like to produce 3 numbers exclusively among numbers in myList.

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by joon View Post
    I got it by myself.
    good job
    Last edited by r937; Nov 4, 2008 at 06:35.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  11. #11
    SitePoint Evangelist
    Join Date
    Mar 2007
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  12. #12
    SitePoint Evangelist
    Join Date
    Mar 2007
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joon View Post
    I got it by myself.

    ValueList(test.id) is the answer!!!
    Doesn't your database have functions for ordering records randomly and limiting the number of records returned?

    http://www.petefreitag.com/item/466.cfm

  13. #13
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

  14. #14
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by longneck View Post
    Code:
    select city
    from myTable
    where continent=1
    order by rand()
    limit 1
    Thank you, CFStarLight and Longneck.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •