AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

random # question

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
GralaZEN
Guest





PostPosted: Sun Apr 27, 2008 8:17 am    Post subject: random # question Reply with quote

I would like to get 10 random numbers ranging from 1-20 that are each unique. How would I do this? Smile
Back to top
happytodd



Joined: 12 Nov 2007
Posts: 83
Location: South Australia

PostPosted: Sun Apr 27, 2008 8:33 am    Post subject: help Reply with quote

Heres your numbers:
6, 14, 17, 2, 3, 19, 5, 7, 5 and 2.
Thank me later Smile
_________________

Back to top
View user's profile Send private message Visit poster's website
GralaZEN
Guest





PostPosted: Sun Apr 27, 2008 8:47 am    Post subject: Reply with quote

Say what? I mean, in a funciton?
Back to top
Wouther



Joined: 01 May 2007
Posts: 79
Location: The Netherlands

PostPosted: Sun Apr 27, 2008 9:31 am    Post subject: Re: help Reply with quote

happytodd wrote:
Heres your numbers:
6, 14, 17, 2, 3, 19, 5, 7, 5 and 2.
Thank me later Smile
Something like this? Razz

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
View user's profile Send private message
n-l-i-d
Guest





PostPosted: Sun Apr 27, 2008 9:51 am    Post subject: Reply with quote

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





PostPosted: Sun Apr 27, 2008 9:54 am    Post subject: Reply with quote

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

PostPosted: Sun Apr 27, 2008 10:16 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
engunneer



Joined: 30 Aug 2005
Posts: 6847
Location: Pacific Northwest, US

PostPosted: Sun Apr 27, 2008 10:56 pm    Post subject: Reply with quote

obligatory:
Related Comic

_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
[VxE]



Joined: 07 Oct 2006
Posts: 1494

PostPosted: Mon Apr 28, 2008 1:23 am    Post subject: Reply with quote

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
View user's profile Send private message
Wouther



Joined: 01 May 2007
Posts: 79
Location: The Netherlands

PostPosted: Mon Apr 28, 2008 10:32 am    Post subject: Reply with quote

engunneer wrote:
obligatory:
Related Comic

Wouther wrote:
happytodd wrote:
Heres your numbers:
6, 14, 17, 2, 3, 19, 5, 7, 5 and 2.
Thank me later Smile
Something like this? Razz
Wink
_________________
Printing css/html-formatted text
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 6264

PostPosted: Mon Apr 28, 2008 12:54 pm    Post subject: Reply with quote

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 Shocked

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
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6847
Location: Pacific Northwest, US

PostPosted: Mon Apr 28, 2008 3:55 pm    Post subject: Reply with quote

Wouther wrote:
Wouther wrote:
Something like this? Razz
Wink


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
View user's profile Send private message Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Mon Apr 28, 2008 4:44 pm    Post subject: Reply with quote

Search the Forum! There is a very long discussion about this topic here.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group