468,245 Members | 1,646 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,245 developers. It's quick & easy.

how to generate random number???

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
3 5905
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
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

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.

Similar topics

2 posts views Thread by Laphan | last post: by
15 posts views Thread by John Cassidy | last post: by
12 posts views Thread by Jim Michaels | last post: by
20 posts views Thread by jjmillertime | last post: by
24 posts views Thread by pereges | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.