SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  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)

    geting multiple number in random

    Code:
    <cfset myNumber=randRange(1,5)>
    The code above get a number in random from 1 to 5.

    I like to get 3 numbers from 1 to 5 in random.

    The would-be code below doesn't work correctly, but it will show what I want.

    Code:
    would-be code
    
    <cfset myNumber=randRange(1,5,3)>
    
    target
    
    myNumber1 = 3
    myNumber2 = 5
    myNumber3 = 2
    Last edited by dotJoon; Dec 3, 2008 at 14:42.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by joon View Post
    Code:
    <cfset myNumber=randRange(1,5)>
    The code above get a number in random from 1 to 5.

    I like to get 3 numbers from 1 to 5 in random.
    joon, come on, i ~know~ you can do this on your own without our help

    please, try harder

    <CFSET myNumber1=randRange(1,5)>
    <CFSET myNumber2=randRange(1,5)>
    <CFSET myNumber3=randRange(1,5)>

    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)
    Quote Originally Posted by r937 View Post
    <CFSET myNumber1=randRange(1,5)>
    <CFSET myNumber2=randRange(1,5)>
    <CFSET myNumber3=randRange(1,5)>
    Sorry, I didn't mention about exclusively.


    Your code can produce same numbers.

    for example,

    I don't like the following result.
    Code:
    myNumber1=3
    myNumber2=1
    myNumber3=3

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    okay, here's an idea, i will give you the idea and you have to create the code for it

    you are good at creating code, right?

    get three random numbers

    if one of them is like the others, throw it away and get another random number

    keep doing that until you have three exclusive random numbers

    simple, yes?

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

  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)
    what's wrong in the code below. Should I make it another way?


    Code:
    <cfset myNumber1=randRange(1,5)>
    <cfset myNumber2=randRange(1,5)>
    
    
    
    <cfloop condition='myNumber1=myNumber2'>
    <cfset myNumber2=randRange(1,5)>
    </cfloop>
    It seems turn infinitely.
    At last it causes java.lang.OutOfMemoryError.

  6. #6
    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:
    <cfset myNumber1=randRange(1,5)>
    <cfset myNumber2=randRange(1,5)>
    
    <cfloop from='1' to='99' index='i'>
    <cfif myNumber1 is myNumber2>
        <cfset myNumber2=randRange(1,5)>
    <cfelse>
        <cfbreak>
    </cfif>
    </cfloop>
    
    <cfset myNumber3=randRange(1,5)>
    
    <cfloop from='1' to='99' index='i'>
    
    <cfif myNumber1 is myNumber3 or myNumber2 is myNumber3>
        <cfset myNumber3=randRange(1,5)>
    <cfelse>
        <cfbreak>
    </cfif>
    
    </cfloop>
    The code above seems work fine.

    But I don't like the temperal number 99.
    What if the randRange(1,100) ?

    How can I make it without the arbitrary number 99?

  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)
    The following code is the answer!

    Code:
    <cfloop condition='myNumber1 is myNumber2'>
    <cfset myNumber2=randRange(1,5)>
    </cfloop>

  8. #8
    SitePoint Zealot YAHA's Avatar
    Join Date
    Jan 2008
    Location
    MD, US
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This thread is hilarious !!! I have had a good laugh by the time I got to the bottom.


    P.S. Try randomly creating 3 numbers and then comparing them. If at least 2 are equal, recreate the whole set. It will probably take more processing time since the probability of selecting a number different from the first two selected is much higher than that of selecting 3 distinct numbers out of 5.

  9. #9
    SitePoint Enthusiast Paul_K's Avatar
    Join Date
    Jul 2008
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <cfset list="1,2,3,4,5" />
    <cfloop from="1" to="3" index="i">
    <cfset ranDom = RandRange(1,listLen(list)) />
    <cfoutput>#listGetAt(list,ranDom)#<br /></cfoutput>
    <cfset list = listDeleteAt(list,ranDom) />
    </cfloop>

    3 loop cycles, 3 random number all unique.


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
  •