# How to get subset of numbers into random order

## Recommended Posts

Hello,

I've got a set of numbers 1 through 20.

From that set, I want a certain subset (ie 1 through 5) to printed in a random order.

I do not want any duplicate numbers in the result, for example not 2, 4, 5, 2, 1

Is there any addition to this kind of little script that will get that result?

The kinds of results I want are like this:

1, 4, 2, 3, 5

2, 5, 3, 1 ,4

4, 2, 3, 5, 1

Preferably, I do not want to have to use an array at this time.

How can I prevent repetitions of a number from showing up in the resulting five numbers?

Local \$w = 0

While \$w < 5
Sleep(10)
\$i = Random(1, 20, 1)
Switch \$i
Case \$i = 1, 2, 3, 4, 5
MsgBox(0, '', \$i, 1)
Case Else
ContinueLoop
EndSwitch
\$w = \$w + 1
WEnd

Thanks for any ideas for me to try.

frew

##### Share on other sites

The solution using an array is very simple.

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

The solution using an array is very simple.

Could 1 through 20 be put into an array, and then give me the results of 1 through 5,

where 1 through 5 are displayed in random order, and without a 1, 2, 3, 4, or 5 showing up

two or more times in the results?

Any ideas on how to do that?

Thank you,

frew

##### Share on other sites

This way works.

;
Local \$w = 0, \$i, \$j = ""

While \$w < 5
Sleep(10)
\$i = Random(1, 20, 1)

Switch \$i
Case 1, 2, 3, 4, 5
If StringInStr(\$j, \$i) = 0 Then
\$j &= \$i & ", "
\$w += 1
EndIf
Case Else
ContinueLoop
EndSwitch
WEnd
MsgBox(0, '', StringTrimRight(\$j, 2))
;
##### Share on other sites

This way works.

Malkey, Thanks so much!

Yes, that works great.

Sorry I couldn't get back to you sooner. I was trying to see how your code works.

I'm not familiar with StringInStr so I'm still checking it out.

Thanks again so much for the help with this. I'm learning from your code example which provides the

exact results I was hoping for.

I hadn't thought to try StringInStr with numbers.

frew

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...