Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Random Numbers 3

Status
Not open for further replies.

dreamstreet

Programmer
Jan 24, 2003
25
0
0
US
Hello,

I want to generate some random numbers and add it to a column called 'epass'. I was wondering if mySQL could do this. Any help will be appreciated.

-Aaron
 
Just to clarify - RAND() produces a random floating-point number betwen 0 and 1, so you might want to scale it appropriately, for example:
[tt]
UPDATE tbl SET epass=epass+RAND()*1000
[/tt]
 
Thank You for your reply. Question: Is it able to create a-z letters? Thanks.
 
You would have to generate an integer between 1 and 26 and then convert that to a char. You can read the manual just as well as I can, I think the functions would be int() in the math section and char() or ascii() in the string functions.
 
Just change the string's character number
here for the ex it is set to 8

<?php
// Our string will have 8 characters, but you can easy change
for($i = 0; $i < 8; $i++) {
// All the letters and numbers
$type[1] = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|X|W|Y|Z";
$type[2] = "0|1|2|3|4|5|6|7|8|9";

// Change for letter or number
$randType = rand(1, 2);

// Get an character alone
$type = explode("|", $type[$randType]);
// Get the total size of characters
$max = count($type);

// Randonic character
$randChar = rand(0, $max);
$code .= $type[$randChar];
}

// Print the password
echo "the password generated was: ".$code;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top