# Thread: geting multiple number in random

1. ## 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```

2. Originally Posted by joon
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

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

3. Originally Posted by r937
<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. 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?

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

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

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

#### Posting Permissions

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