[array] Quantum Shuffle array ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

[array] Quantum Shuffle array ?

12 Jul 2018, 06:48

Hello,

This website offers true random numbers and strings to anyone on the internet.
http://qrng.anu.edu.au/

How to shuffle array of countries with a quantum random generated string ? :think:

Code: Select all

msgbox, How to shuffle array of countries with a quantum random generated string ?

countries:=["Afghanistan","Albania","Algeria","Andorra","Angola","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas, The","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bhutan","Bolivia","Bosnia and Herzegovina","Botswana","Brazil","Brunei","Bulgaria","Burkina Faso","Burma","Burundi","Cambodia","Cameroon","Canada","Cabo Verde","Central African Republic","Chad","Chile","China","Colombia","Comoros","Congo, Democratic Republic of the","Congo, Republic of the","Costa Rica","Cote d'Ivoire","Croatia","Cuba","Curacao","Cyprus","Czechia","Denmark","Djibouti","Dominica","Dominican Republic","East Timor","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Eswatini","Ethiopia","Fiji","Finland","France","Gabon","Gambia, The","Georgia","Germany","Ghana","Greece","Grenada","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Holy See","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati","Korea, North","Korea, South","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Morocco","Mozambique","Namibia","Nauru","Nepal","Netherlands","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestinian Territories","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Qatar","Romania","Russia","Rwanda","Saint Kitts and Nevis","Saint Lucia","Saint Vincent and the Grenadines","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Sint Maarten","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","Sudan","Suriname","Swaziland (See Eswatini)","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor-Leste","Togo","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam","Yemen","Zambia","Zimbabwe"]


try
{
	urlPage := UrlDownloadToVar("http://qrng.anu.edu.au/RawChar.php")
}
catch
{
	urlPage := "Couldn't load!"
}


UrlDownloadToVar(URL) {
	WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WebRequest.Open("GET", URL)
	WebRequest.Send()
	Return WebRequest.ResponseText
}


loop, parse, urlpage, `n
if (StrLen(a_loopfield)>=1024) {
QuantumString:=substr(a_loopfield,1,1024)
break
}

msgbox,,1024 online random alphanumeric (and underscore) characters., % QuantumString

msgbox, % st_printArr(countries)

st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}
Thanks :?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [array] Quantum Shuffle array ?  Topic is solved

13 Jul 2018, 05:28

Hello.
Here is a simple array shuffle, with example,

Code: Select all

shuffle(arr, rng){
	local 
	i := arr.length()
	loop % i - 1
		swap(arr, i, rng.call(i)), --i
	return arr
}
swap(arr, a, b){
	local
	c := arr[a], arr[a] := arr[b], arr[b] := c
}

; Example

countries:=["Afghanistan","Albania","Algeria","Andorra","Angola","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas, The","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bhutan","Bolivia","Bosnia and Herzegovina","Botswana","Brazil","Brunei","Bulgaria","Burkina Faso","Burma","Burundi","Cambodia","Cameroon","Canada","Cabo Verde","Central African Republic","Chad","Chile","China","Colombia","Comoros","Congo, Democratic Republic of the","Congo, Republic of the","Costa Rica","Cote d'Ivoire","Croatia","Cuba","Curacao","Cyprus","Czechia","Denmark","Djibouti","Dominica","Dominican Republic","East Timor","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Eswatini","Ethiopia","Fiji","Finland","France","Gabon","Gambia, The","Georgia","Germany","Ghana","Greece","Grenada","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Holy See","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati","Korea, North","Korea, South","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Morocco","Mozambique","Namibia","Nauru","Nepal","Netherlands","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestinian Territories","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Qatar","Romania","Russia","Rwanda","Saint Kitts and Nevis","Saint Lucia","Saint Vincent and the Grenadines","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Sint Maarten","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","Sudan","Suriname","Swaziland (See Eswatini)","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor-Leste","Togo","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam","Yemen","Zambia","Zimbabwe"]

msgbox % st_printArr(shuffle(countries, func("rand"))) 	; uses ahk's rng, faster.
msgbox % st_printArr(shuffle(countries, func("qrandnorm")))

qrandnorm(i){
	static Q := []
	if !Q.length()
		Q := getQ()
	return mod(Q.pop(), i) + 1
}

getQ(n := "512", type := "uint16"){
	local
	url := "https://qrng.anu.edu.au/API/jsonI.php?length=" . n . "&type=" . type
	try 
		urlPage := UrlDownloadToVar(url)
	catch e {
		msgbox % e.message "`n`nScript closing"
		exitapp
	}
	regexmatch(urlPage, "O).*\[(.+)\]", n)
	return strsplit(n.1, ",")
}

rand(i){
	local
	random r, 1, i
	return r
}

UrlDownloadToVar(URL) {
	WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WebRequest.Open("GET", URL)
	WebRequest.Send()
	Return WebRequest.ResponseText
}



st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}
The http-request times out sometimes.

Cheers.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [array] Quantum Shuffle array ?

13 Jul 2018, 19:17

Awesome, works fine
Helgef wrote:shuffle(countries, func("rand") ; uses ahk's rng, faster.
shuffle(countries, func("qrandnorm")
Awesome, works fine :bravo:
I needed true random shuffle to compare it with a pseudo-random shuffle. (See article below) 8-)

WHY 'RANDOM' SHUFFLE FEELS FAR FROM RANDOM
https://www.independent.co.uk/life-styl ... 66621.html

Cheers
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [array] Quantum Shuffle array ?

14 Jul 2018, 00:06

There is no observable difference, since both rngs are uniformly distributions, and the code only uses one shuffle algorithm, in fact, it's Fisher ;) . Spotify changed the shuffle algorithm not the rng.

Cheers.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [array] Quantum Shuffle array ?

14 Jul 2018, 09:45

Helgef wrote:the code only uses one shuffle algorithm, in fact, it's Fisher
Do you mean that Fisher-Yates algorithm is not affected by the quality of the generated random number ? :think:

I always use this shuffle algorithm witch is a modified and simplified version from the one by Flibio.
https://github.com/flibioahk/ishido

Is that Fisher-Yates ?
Help wrote:Reseeding can improve the quality/security of generated random numbers, especially when NewSeed is a genuine random number rather than one of lesser quality such as a pseudo-random number.
I also disabled reseeding ;Random,, A_TickCount because i noticed that the shuffled array was from poor quality when constantly reseeding.

Code: Select all

ShuffleArray(ByRef arr)
{
  ; Random,, A_TickCount
    elems:=[]
    loop, % arr.Length() 
    {
	 Random, rnd, 1, % arr.MaxIndex()
	 elems[A_Index] := arr.RemoveAt(rnd)
    }
    arr:=elems
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [array] Quantum Shuffle array ?

14 Jul 2018, 10:05

You should not reseed constantly.
A randomly generated number is calculated using its seed. If you constantly set the seed to a similar value you will get similar results.
You can get 461 random 32 bit integers from a single seed before you need a new one (I think it was 461 see the remarks on the Random command help page.)
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [array] Quantum Shuffle array ?

14 Jul 2018, 15:41

Do you mean
no
Is that Fisher-Yates ?
yes
I also disabled reseeding
If you think you need to reseed, you don't, if you know you need to reseed, you won't, because you will use another language. If you know that you need to reseed, based on reading the docs, you neither know nor need.

Generally speaking, only reseed for reproducibility purposes.

Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jaka1, mikeyww and 182 guests