Jump to content

Return random characters


slpctrl

Recommended Posts

How would I return any 5 random characters from a string? Let's say I've got this:

 

 

<?php
$str1 = md5(microtime() * mktime());
$str2 = str_shuffle($str1);
$str = substr($str2,0,5);
?>

 

line 1 gets a string of random characters

line 2 shuffles those random characters

line 3 gets the first 5 characters

 

 

Instead of getting the first 5 characters, how can I return 5 random characters?

Link to comment
https://forums.phpfreaks.com/topic/122945-return-random-characters/
Share on other sites

you are already returning 5 random characters. str_shuffle makes the string random, then you take the first 5 characters. it literally can't get 'more random' than that. if it makes you feel better, you could use str_shuffle again, then take the first 5 characters. but since str_shuffle already randomized the first 5 chars, it's a waste of effort. grad level stats, don't you know.  :)

you are already returning 5 random characters. str_shuffle makes the string random, then you take the first 5 characters. it literally can't get 'more random' than that. if it makes you feel better, you could use str_shuffle again, then take the first 5 characters. but since str_shuffle already randomized the first 5 chars, it's a waste of effort. grad level stats, don't you know.  :)

 

Yeah that's what I was thinking. Thanks I'm dumb  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.