Programming question
Programming question
I have an arraylist of lets say 200 passwords
Now I want to put randomly in another array 50 unique passwords from the password list.
What's the best way to do this something like below or has anyone a better(faster) solution.
for passwords = 0 to maxpasswords
for uniquepasswords = 0 to 50
next
next
Now I want to put randomly in another array 50 unique passwords from the password list.
What's the best way to do this something like below or has anyone a better(faster) solution.
for passwords = 0 to maxpasswords
for uniquepasswords = 0 to 50
next
next
Cor de Visser
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
hey cor, isn't it easier to use a linked list for this?
say, you've got 200 paswords:
pasword_n.l = 200
random_pick.l = random(password_n-1)
selectelement(list(),random_pick)
addelement()
list() = whatever
(just from the top of my head, that's no real code above
)
say, you've got 200 paswords:
pasword_n.l = 200
random_pick.l = random(password_n-1)
selectelement(list(),random_pick)
addelement()
list() = whatever
(just from the top of my head, that's no real code above

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Hello blueznl,
The 50 choosen passwords must be unique, it must be done with arrays
The 50 choosen passwords must be unique, it must be done with arrays
Cor de Visser
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
someting like this could maybe help?
Not real code, just quickly type
Not real code, just quickly type

Code: Select all
for uniquepasswords = 0 to 200
array(1)=0
next
uniquepasswords = 0
Repeat
pick=random(200)
if array(1)= 0 ; not yes picked
array(1)=1
uniquepasswords + 1
endif
until uniquepasswords = 50
hey bericko, once you add passwords, the array get bigger, so your random should reflect that 

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Cor, your question is very interesting, it is not trivial to make a "perfect" and the fastest algorithm to extract randomly a number (50 in this case) of items from a "bag" of a bigger number of items (200 in this case).
It is needed to put brains to work to do it :roll:
For me is interesting, I'll try.
It is needed to put brains to work to do it :roll:
For me is interesting, I'll try.
ooops, cor, i didn't read your question properly, tonight i'll give it another try 
(effe knutselen)

(effe knutselen)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
see personal message
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
oh zo... 
this will do it...

this will do it...
Code: Select all
password_n = 500
user_n = 200
;
Dim passwords.s(password_n)
Dim gekozenpasswords.s(user_n)
;
For n = 1 To password_n
passwords(n) = "watdanook"+Str(n)
Next n
;
For n = 1 To user_n
r = Random(password_n-1)+1
gekozenpasswords(n) = passwords(r)
passwords(r) = passwords(password_n)
password_n = password_n-1
Next n
;
For n = 1 To user_n
Debug gekozenpasswords(n)
Next n
;
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 537
- Joined: Wed Oct 29, 2003 10:35 am
I like this suggestion.... it seems the best.... all is neede is to delete the element selected from the list of 200 then your next random will be to select from 199 etc.....blueznl wrote:hey cor, isn't it easier to use a linked list for this?
say, you've got 200 paswords:
pasword_n.l = 200
random_pick.l = random(password_n-1)
selectelement(list(),random_pick)
addelement()
list() = whatever
(just from the top of my head, that's no real code above)
Surely would be most efficient ?!
Paid up PB User !
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Hi blueznl,
Sorry but it doesn't, I checked this way :
The reason behind it is that it is very unlikely that a FLOAT random number generator will give 200 different INT numbers (obtained by discarding the decimal part) when called 200 times in a row ...
For an int random number generator which would work may I suggest a look at :
http://www.math.keio.ac.jp/~matumoto/emt.html
Bests.
Stan.
this will do it...
Code:
password_n = 500
user_n = 200
;
Dim passwords.s(password_n)
Dim gekozenpasswords.s(user_n)
;
For n = 1 To password_n
passwords(n) = "watdanook"+Str(n)
Next n
;
For n = 1 To user_n
r = Random(password_n-1)+1
gekozenpasswords(n) = passwords(r)
passwords(r) = passwords(password_n)
password_n = password_n-1
Next n
;
For n = 1 To user_n
Debug gekozenpasswords(n)
Next n
;
Sorry but it doesn't, I checked this way :
Code: Select all
OpenConsole( )
; blueznl code (no change )
password_n = 500
user_n = 200
;
Dim passwords.s(password_n)
Dim gekozenpasswords.s(user_n)
;
For n = 1 To password_n
passwords(n) = "watdanook"+Str(n)
Next n
;
For n = 1 To user_n
r = Random(password_n-1)+1
gekozenpasswords(n) = passwords(r)
passwords(r) = passwords(password_n)
password_n = password_n-1
Next n
; End of blueznl code
; Check if passwords are all different
Ok = 1
For n = 1 To user_n - 1
For m = n + 1 To User_n
If FindString(gekozenpasswords(n), gekozenpasswords(m), 1)
Ok = 0
EndIf
Next m
Next n
If Ok
PrintN( "It works" )
Else
PrintN( "It doesn't work" )
EndIf
Input( )
CloseConsole( )
For an int random number generator which would work may I suggest a look at :
http://www.math.keio.ac.jp/~matumoto/emt.html
Bests.
Stan.