Programming question

Just starting out? Need help? Post your questions and find answers here.
Cor
Enthusiast
Enthusiast
Posts: 124
Joined: Fri Apr 25, 2003 7:52 pm
Location: Netherlands
Contact:

Programming question

Post by Cor »

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
Cor de Visser

Registered PureBasic user

Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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 :-))
( 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... )
Cor
Enthusiast
Enthusiast
Posts: 124
Joined: Fri Apr 25, 2003 7:52 pm
Location: Netherlands
Contact:

Post by Cor »

Hello blueznl,

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
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

someting like this could maybe help?
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
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

hey bericko, once you add passwords, the array get bigger, so your random should reflect that :P
( 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... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

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.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

ooops, cor, i didn't read your question properly, tonight i'll give it another try :-)

(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... )
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Berikco's suggestion will work but run time is random (sort-of). Best way is to put your pw's in a list and use a random number 0..ListCount-1 to select one then remove the selection. Think of a dealing from a deck of cards.
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

dmoc wrote:Berikco's suggestion will work but run time is random (sort-of). Best way is to put your pw's in a list and use a random number 0..ListCount-1 to select one then remove the selection. Think of a dealing from a deck of cards.
Yup, list is much better, but seems Cor wanted to use array.
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Yeah, but he could use a temp list or would you say that's cheating? :D

PS: And anyway he's clearly schizo... "arraylist" :lol:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

oh zo... :-)

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... )
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

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 :-))
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.....

Surely would be most efficient ?!
Paid up PB User !
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

blueznl, your algorithm is fast, even it destroys the content of the source array.

I have the same question than dontmailme:
Surely would be most efficient ?!



NOTE for blueznl: please use english words in code too, because code should be much more quickly understandable. :)
stan
User
User
Posts: 29
Joined: Tue Jun 10, 2003 2:46 pm

Post by stan »

Hi blueznl,
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( )

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.
Post Reply