Programming question

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

stan, read your checkcode carefully, it's wrong :-)

password(1) could be "secret1"

while password(2) coud be "secret100"

the way you check would give a false positive! :-)

you gotta change one line to:

Code: Select all

If gekozenpasswords(n) = gekozenpasswords(m)

( 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... )
stan
User
User
Posts: 29
Joined: Tue Jun 10, 2003 2:46 pm

Post by stan »

Blueznl,

You are perfectly right (and your code is right as well :) :) :) ) please do accept my apologies.

I wonder however how random is the random function : I ran this code for more than 130 000 iterations :

Code: Select all

OpenConsole( )

Ok = 1
i = 1
min_min = 0
max_max = 1000

Repeat

  password_n = 500 
  user_n = 200 
  
  max = 0
  min = 1000
  
  Dim passwords(password_n) 
  Dim gekozenpasswords(user_n) 
   
  For n = 1 To password_n 
    passwords(n) = 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 
    If gekozenpasswords(n) > max
      max = gekozenpasswords(n)
    endif  
    If gekozenpasswords(n) < min
      min = gekozenpasswords(n)
    EndIf
  Next n 
  
  If min_min < min
    min_min = min
  EndIf
  
  If max_max > max
    max_max = max
  EndIf  
  
  PrintN( "i = " + Str( i ) + "    max_max " + Str( max_max ) + "    min_min " + Str( min_min ) )
  i = i + 1 
  
Until ok = 0
 
CloseConsole( )
And got min_min = 26, max_max = 478 which is a bit strange for a uniform random generator ... Fred ???

Again my apologies.

Bests.

Stan.
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Dunno if this has something to do with what you're trying to achieve (coz I haven't followed the thread),
but random is not very random without giving it a random seed...

Code: Select all

RandomSeed(gettickcount_())
-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Here's a couple of things to remember ...

#1) A random number is pseudo random, I think it is generating using the system timer as a seed by default. Next you should use the command

Code: Select all

RandomSeed(Value) 
and set the value to some obscure dynamic parameter maybe a multiplication of gettickcount_() * timegettime_(). This should produce a different seed every time ... or you could use a more precise timer for more varied results.

#2) If you do not use linked lists, you will have to use a loop, much more inefficient to check for unique passwords. But if you used linked lists you would have to do a loop and check to see if the password is unique anyways, unless you make a copy of the original linked list, and restore it after. But that is also inefficient. I would go with static arrays and a loop for checking almost like Berikco first suggested.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Sorry LarsG, didn't catch your post :lol: (randomseed)
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Codemonger wrote:Sorry LarsG, didn't catch your post :lol: (randomseed)
I keep my posts short.. :wink:

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

LarsG wrote:
Codemonger wrote:Sorry LarsG, didn't catch your post :lol: (randomseed)
I keep my posts short.. :wink:
i would too if my ego wouldn't need so much space...

8)
( 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... )
Post Reply