TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-10-2008, 11:24 PM   #1 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default Eyes Random String Generator

Well i put this little baby together for a project of my im working on, and thought i share it with you all.

Not the moast advanced but does what i wanted.
Thereīs room for alot of improvement i would guess.

Have fun :)

PHP Code:
function myRandomStr($passLen=16$shakes=5)
{
    
// Get some letters and integers easy and quick into arrays
    
$smal_letters range('a','z');
    
$big_letters range('A','Z');
    
$integers range('0''9');

    
// merge thoose arrays above to one array
    
$combined array_merge($smal_letters$big_letters$integers);
    
    
// shake it around abit
    
for($s 0$s intval($shakes); ++$s)
    {
        
shuffle($combined);
    }
    
    
// find out how big the array is
    
$len count($combined);

    
// then finaly create the string
    
for($i 0$i intval($passLen); ++$i)
    {
        
// Choose some random letters from the array
        
$salt mt_rand(0$len);
        echo(
$combined[$salt]);
    }
}

// Lets try a 15 char long and with 5 shakes.
myRandomStr(155); 
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
The Following 2 Users Say Thank You to EyeDentify For This Useful Post:
sketchMedia (06-11-2008), Village Idiot (06-11-2008)
Old 06-11-2008, 12:20 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Very cool.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
EyeDentify (06-11-2008)
Old 06-11-2008, 06:07 AM   #3 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Thx. Village Idiot :)
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 06-11-2008, 09:09 AM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Nice script, thanks for sharing.

One thing however, there is a mistake, the script generates a 'undefined offset' because you have set the max value of mt_rand to '62' (number of elements in the array) so every now and again you will get the '62' being produced and used as a key for the array, however the maximum array key value is only '61' (php is zero indexed so the first value is 0 not 1) thus the key '62' doesn't exist.
to rectify just subtract 1 from the max value of mt_rand

PHP Code:
<?php
function myRandomStr($passLen=16$shakes=5)
{
    
// Get some letters and integers easy and quick into arrays
    
$smal_letters range('a','z');
    
$big_letters range('A','Z');
    
$integers range('0''9');

    
// merge thoose arrays above to one array
    
$combined array_merge($smal_letters$big_letters$integers);
    
    
// shake it around abit
    
for($s 0$s intval($shakes); ++$s)
    {
        
shuffle($combined);
    }
    
    
// find out how big the array is
    
$len count($combined) - 1;
    
// then finaly create the string
    
for($i 0$i intval($passLen); ++$i)
    {
        
// Choose some random letters from the array
        
$salt mt_rand(0$len);
        echo 
$combined[$salt];
    }
}

// Lets try a 15 char long and with 5 shakes.
myRandomStr(155);
that should now run correctly.

apart from that its nice, thanks m8.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-11-2008 at 11:10 AM. Reason: removed mistake in last loop, see salathe's post below
sketchMedia is offline  
Reply With Quote
Old 06-11-2008, 09:21 AM   #5 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

@sketch media

Thx. I just tought of that little misstake myself :)

It botherd me when i went to bed lastnight. Cause there were something i missed in the code. And that might be it.

Thx for reminding me :)
Quote:
I want to point out that $passLen has nothing to do with the array. Just the lenght of the string returned.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 06-11-2008, 10:31 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I would advise making the function somewhat more efficient. There are a number of things which, unless you can justify their inclusion or the way they've been done, I feel are useless or could be better implemented.

First, the method of constructing the array of characters which can be used in the generated string. You make four function calls (range, array_merge) simply to construct a single array of characters a-zA-Z0-9. This array will always be the same upon every function call so why not just hard-code?

Next, the for/shuffle lines. What advantage does shuffling the array 5, 10, 50 times have over doing it once? Does it make the array more random? I'd say this whole section is unnecessary since you're later choosing array keys at random anyway (with mt_rand).

I'd also suggest returning the resulting string rather than echoing out each character as the function will be much more versatile (who says you need to output the string?).

Sketch, thanks for the note about the off-by-one error on $len however you introduce one yourself by changing the final for loop to use intval($passLen-1) (the resulting random string will always be one character less than $passLen).

I think this function could well be refactored into something maybe 10 times faster to run and equally so easy to glance at and understand what's going on.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
EyeDentify (06-11-2008)
Old 06-11-2008, 11:09 AM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Sketch, thanks for the note about the off-by-one error on $len however you introduce one yourself by changing the final for loop to use intval($passLen-1) (the resulting random string will always be one character less than $passLen).
Its not an undefined offset, but yes thanks for noticing that, i didn't mean to post the code with that line in, i must have left it in when i was messing with the code earlier no idea why mind but there you go.

note to self: learn how to use copy and paste properly
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-11-2008, 11:15 AM   #8 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
I would advise making the function somewhat more efficient. There are a number of things which, unless you can justify their inclusion or the way they've been done, I feel are useless or could be better implemented.

First, the method of constructing the array of characters which can be used in the generated string. You make four function calls (range, array_merge) simply to construct a single array of characters a-zA-Z0-9. This array will always be the same upon every function call so why not just hard-code?

Next, the for/shuffle lines. What advantage does shuffling the array 5, 10, 50 times have over doing it once? Does it make the array more random? I'd say this whole section is unnecessary since you're later choosing array keys at random anyway (with mt_rand).

I'd also suggest returning the resulting string rather than echoing out each character as the function will be much more versatile (who says you need to output the string?).

Sketch, thanks for the note about the off-by-one error on $len however you introduce one yourself by changing the final for loop to use intval($passLen-1) (the resulting random string will always be one character less than $passLen).

I think this function could well be refactored into something maybe 10 times faster to run and equally so easy to glance at and understand what's going on.
Your right. There are plenty of things that could be done in another more effective way.

This how ever were just a short bare bone code snippet that anyone could base there random string functions upon if they wish. I wasn't out to publish a complete solution, just another way to do things. Maybe not optimal but it works.

The reason i used the approach with the range() to get chars into arrays and then merge them was to be able to add other ranges() or manually constructed arrays with chars to mix with.

And maybe the shuffle() isīnt necessary.
But advantage you say ?, it makes the array more random.
And when you later down the code pick random chars.
Well that's just more random candy for ya :)

I little sharing if you like.

i admit that i should of course have made the function return the string and not echo().

This optimal performance hysteria goes to far some times.
Hardware these days can do a lot more then we tend to give it credit for. And of course itīs also a question of how often one use the said function.

To conclude. I appreciate your insight and shall remember it in future adventures.

/Eye
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 06-11-2008, 11:33 AM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
This optimal performance hysteria goes to far some times.
Hardware these days can do a lot more then we tend to give it credit for. And of course itīs also a question of how often one use the said function.
Perhaps, but in my opinion it doesn't mean that you shouldn't optimize everything you do.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-11-2008, 01:21 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I'm not really talking about optimising the code, in the sense of squeezing out that last microsecond of runtime, or saving that extra CPU cycle here and there. My main point was that much of what you wrote appears to have been unnecessary for the functionality of the code. Case in point, the multiple calls to shuffle.

From my understanding (which may be wrong!), shuffling an array multiple times doesn't make the values inside it any more randomly placed than simply shuffling once -- I'd be happy to see any tests done to determine the actual behaviour, if indeed the array is 'more random'. Because of that, I feel that shuffling more than once isn't necessary. Indeed, the entire point of shuffling the array (to randomise the values) is a non-point because the values are selected at random later using mt_rand. What's the advantage of selecting a random value from a shuffled array over selecting a random value from an ordered array? I don't see any, maybe you do.

I'll repeat, I'm not looking to optimise the code for ultimate performance but instead trying to get across the idea of good practises. There's no good point that I can think of (feel free to point any out!) as to why it's a good idea to shuffle the array once, twice, twenty times.

Take out the code block under "shake it around abit" and does the function perform any differently? Are the echoed/returned strings any less random?
Salathe is offline  
Reply With Quote
Old 06-11-2008, 05:11 PM   #11 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
I'm not really talking about optimising the code, in the sense of squeezing out that last microsecond of runtime, or saving that extra CPU cycle here and there. My main point was that much of what you wrote appears to have been unnecessary for the functionality of the code. Case in point, the multiple calls to shuffle.

From my understanding (which may be wrong!), shuffling an array multiple times doesn't make the values inside it any more randomly placed than simply shuffling once -- I'd be happy to see any tests done to determine the actual behaviour, if indeed the array is 'more random'. Because of that, I feel that shuffling more than once isn't necessary. Indeed, the entire point of shuffling the array (to randomise the values) is a non-point because the values are selected at random later using mt_rand. What's the advantage of selecting a random value from a shuffled array over selecting a random value from an ordered array? I don't see any, maybe you do.

I'll repeat, I'm not looking to optimise the code for ultimate performance but instead trying to get across the idea of good practises. There's no good point that I can think of (feel free to point any out!) as to why it's a good idea to shuffle the array once, twice, twenty times.

Take out the code block under "shake it around abit" and does the function perform any differently? Are the echoed/returned strings any less random?
I got ya ;)

I see what you mean.
It would be best to remove the shuffle() thing.

I guess i just was a happie coder getting a little crazy burning the midnight oil :)
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:27 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design