 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
GralaZEN Guest
|
Posted: Sun Apr 27, 2008 8:17 am Post subject: random # question |
|
|
I would like to get 10 random numbers ranging from 1-20 that are each unique. How would I do this?  |
|
| Back to top |
|
 |
happytodd
Joined: 12 Nov 2007 Posts: 83 Location: South Australia
|
Posted: Sun Apr 27, 2008 8:33 am Post subject: help |
|
|
Heres your numbers:
6, 14, 17, 2, 3, 19, 5, 7, 5 and 2.
Thank me later  _________________
 |
|
| Back to top |
|
 |
GralaZEN Guest
|
Posted: Sun Apr 27, 2008 8:47 am Post subject: |
|
|
| Say what? I mean, in a funciton? |
|
| Back to top |
|
 |
Wouther
Joined: 01 May 2007 Posts: 79 Location: The Netherlands
|
Posted: Sun Apr 27, 2008 9:31 am Post subject: Re: help |
|
|
| happytodd wrote: | Heres your numbers:
6, 14, 17, 2, 3, 19, 5, 7, 5 and 2.
Thank me later  | Something like this?
| Code: | Loop, 10 ;How many numbers?
{
i := A_Index ;Save it: A_Index will be changed because we use another loop
Random, nr%i%, 1, 20 ;Generate a number
Loop, %i% - 1 ;Loop through all previous numbers
If (nr%i% = nr%A_Index%) ;Compare the newly found number with the previous ones. If they're equal,...
Loop
{
Random, nr%i%, 1, 20 ;...generate a new number...
If (nr%i% != nr%A_Index%) ;...until they're not...
Break ;...then break
}
}
i= |
_________________ Printing css/html-formatted text |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Sun Apr 27, 2008 9:51 am Post subject: |
|
|
Something like this?
| Code: | MsgBox % GetUniqueRandomNrs(10, 1, 20, "|")
GetUniqueRandomNrs(count, min, max, delim)
{
nrs = ; empty nrs
Loop %count%
{
GetUniqueRandom: ; sub-routine
Random, anr, %min%, %max%
If anr in %nrs% ; exists
Gosub, GetUniqueRandom
else
nrs := nrs . "," . anr ; add to nrs
}
StringReplace, nrs, nrs, `,, delim, All ; replace comma's with delimiter
Return nrs
}
|
not-tested |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Sun Apr 27, 2008 9:54 am Post subject: |
|
|
Sorry
| Code: | | StringReplace, nrs, nrs, `,, delim, All ; replace comma's with delimiter |
should be
| Code: | | StringReplace, nrs, nrs, `,, %delim%, All ; replace comma's with delimiter |
still not-tested
HTH |
|
| Back to top |
|
 |
John W
Joined: 09 Apr 2007 Posts: 172
|
Posted: Sun Apr 27, 2008 10:16 am Post subject: |
|
|
| Code: | Loop
{
Random, Random, 1, 20
If (Random != Number1 && Random != Number2 && Random != Number3 && Random != Number4 && Random != Number5 && Random != Number6 && Random != Number7 && Random != Number8 && Random != Number9 && Random != Number10)
{
++Index
Number%Index% := Random
If (Index = 10)
Break
}
}
Loop, 10
Numbers:=Numbers ", " Number%A_Index%
Msgbox, %Numbers% |
Bitte sehr. Nicht getestet, sollte aber laufen. _________________ John
Inactive - Until AutoHotkey is available for Linux.
 |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Sun Apr 27, 2008 10:56 pm Post subject: |
|
|
obligatory:
Related Comic
 _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1494
|
Posted: Mon Apr 28, 2008 1:23 am Post subject: |
|
|
Even though I admit the superiority of engunneer's code, I have a function to contribute: | Code: | NpRand( low = 0, High = 0 ) ; Another Scriptlett by [VxE]
{ ; This function will return a random integer between HIGH and LOW
; with the exception of a number that has been previously returned
; by this function. That memory may be reset by calling this function with
; no parameters, or by calling this function with equal parameters
; Each number that may be returned by this function has an equal probability
Static NPRAND_STATIC_LIST
If High Is Not Integer
return 0
If Low Is Not Integer
return 0
If ( low >= high )
{
IfNotEqual, low, %high%, Return, NpRand( high, low )
Random,, %A_Now%
NPRAND_STATIC_LIST := ""
return 0
}
list := "¿"
Loop % high - low + 1
list .= (low + A_Index - 1) . "¿"
Loop, Parse, NPRAND_STATIC_LIST, ¿
StringReplace, list, list, ¿%A_LoopField%¿, ¿
StringReplace, list, list, ¿, ¿, UseErrorLevel
If ErrorLevel < 2
return 0
Random, Rando, 2, %ErrorLevel%
Loop, Parse, list, ¿
If ( A_Index = Rando )
{
Rando := A_LoopField
break
}
NPRAND_STATIC_LIST .= Rando "¿"
return rando
} ; End of NpRand() function
NpRand() ; this is not a strictly necessary line
MsgBox % NpRand( 1, 10 ) "`n" NpRand( 1, 10 ) "`n"
. NpRand( 1, 10 ) "`n" NpRand( 1, 10 ) "`n"
. NpRand( 1, 10 ) "`n" NpRand( 1, 10 ) "`n"
. NpRand( 10, 1 ) "`n" NpRand( 1, 10 ) "`n"
. NpRand( 1, 10 ) "`n" NpRand( 1, 10 ) |
Although, if you call the function again with different bounds, it will still 'remember' numbers it returned from previous calls, so it would be a good idea to reset its memory when changing dice. Feel free to tweak the error handling as desired (left as-is, it will return 0 on most errors )
BTW: a regressive memory method would be much shorter to code, because it would build a string of possible numbers in a static var, then use Sort, var, Random, then remove the top item from the list and return it. _________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
Wouther
Joined: 01 May 2007 Posts: 79 Location: The Netherlands
|
Posted: Mon Apr 28, 2008 10:32 am Post subject: |
|
|
| Wouther wrote: | | happytodd wrote: | Heres your numbers:
6, 14, 17, 2, 3, 19, 5, 7, 5 and 2.
Thank me later  | Something like this? |  _________________ Printing css/html-formatted text |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 6264
|
Posted: Mon Apr 28, 2008 12:54 pm Post subject: |
|
|
A variant of daonlyfreez' ( n-l-i-d ) script:
| Code: | MsgBox % GetUniqueRandomNrs(10, 1, 20 )
GetUniqueRandomNrs( count, min, max ) {
Loop {
random, rnum, %min%, %max%
if instr( rlist, rnum "|" )
continue
rlist .= rnum "|"
ctr ++
ifequal, ctr, %count%, break
}
stringtrimright, rlist, rlist, 1
return rlist
} |
IMHO, the above script will well-suit only if min/max range is very wide, like: 1/9999.
I tried GetUniqueRandomNrs(20, 1, 20 ) and it runs eternally
here is a variant that runs fine when the range is narrow:
| Code: | MsgBox % RandomList( 10,1,20 )
RandomList( nums, min, max, wid=20 ) {
varsetcapacity( spaces, wid, 32 ), ctr := min
Loop % max - min + 1
outstr .= substr( spaces . ctr, 1-wid ) "|", ctr := ctr+1
stringtrimright, outstr, outstr, 1
sort outstr,random d|
outstr := substr( outstr, 1, ( (nums*wid)+nums-1 ) )
stringreplace, outstr, outstr, %a_space%,, All
return outstr
} |
|
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Mon Apr 28, 2008 3:55 pm Post subject: |
|
|
| Wouther wrote: | | Wouther wrote: | Something like this? |  |
Exactly like that. I only skimmed the topic, and since you also had posted useful code, I assumed you were linking to another forum post. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Mon Apr 28, 2008 4:44 pm Post subject: |
|
|
| Search the Forum! There is a very long discussion about this topic here. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|