By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

how to generate random number???

P: n/a
i want to generate a random number of a fixed length

so how can i do this ??

i know some function which returns a single random character at a time
but is there any built-in function which takes length of string and
return a random string of that length??????

can i implement mechanism so that the random number generated is form a
list of characters i specify. e.g. suppose if i want to create a random
string which contains only A-Z and 0-9 then how can i do this ???
thxs for your help in advance.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2 Apr 2005 03:07:32 -0800, "vishal" <vi************@yahoo.co.in> wrote:
i want to generate a random number of a fixed length

i know some function which returns a single random character at a time
but is there any built-in function which takes length of string and
return a random string of that length??????
Sounds a bit too specific to be a built-in function. It's just a combination
of a couple of functions anyway.
can i implement mechanism so that the random number generated is form a
list of characters i specify. e.g. suppose if i want to create a random
string which contains only A-Z and 0-9 then how can i do this ???


<?php
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$length = 10;

$max_i = strlen($chars)-1;
$value = '';
for ($i=0;$i<$length;$i++)
{
$value .= $chars{mt_rand(0,$max_i)};
}

print $value;
?>

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
vishal <vi************@yahoo.co.in> wrote:

Vishal, reading your last 4 question I'm getting under the impresssion
that you are using com.lang.php as some kind of first line helpdesk.
You throw in simple question and expect detailed answers. I'm missing
background information, 2 of the 4 last questions were a simple RTFM on
php.net, the third could be found after a little searching...
i want to generate a random number of a fixed length

so how can i do this ??
An other RTFM http://nl2.php.net/manual/en/function.rand.php when
numbers are concerned (see the min/max args).
i know some function which returns a single random character at a time
but is there any built-in function which takes length of string and
return a random string of that length??????
So why not write a trivial function that returns a string of single
random character appended the specified times?
can i implement mechanism so that the random number generated is form a
list of characters i specify. e.g. suppose if i want to create a random
string which contains only A-Z and 0-9 then how can i do this ???


TIMTOWTDI

IMHO the most simple solution would be to create an array of allowed
chars and pick from the array...

Jul 17 '05 #3

P: n/a

On 2-Apr-2005, "vishal" <vi************@yahoo.co.in> wrote:
i want to generate a random number of a fixed length

so how can i do this ??

i know some function which returns a single random character at a time
but is there any built-in function which takes length of string and
return a random string of that length??????

can i implement mechanism so that the random number generated is form a
list of characters i specify. e.g. suppose if i want to create a random
string which contains only A-Z and 0-9 then how can i do this ???


http://www.php.net/manual/en/function.str-shuffle.php

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.