IT Community - Software Programming, Web Development and Technical Support

To get random number in mysql

This is a discussion on To get random number in mysql within the Database Support forums, part of the Web Development category; How to get 4-digit Random number The following code will generate 4 digit random number. using this code you ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-27-2007, 02:56 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default To get random number in mysql

How to get 4-digit Random number

The following code will generate 4 digit random number.
using this code you can get upto 300 digit random number.

Code:
SET @number:=4;
select cast(FLOOR(concat('1',REPEAT('0',@number-1)) + (RAND() * CONCAT('9',REPEAT('0',@number-1))))as binary);
output

4523
3821
9656
__________________
-Murali..

Last edited by Murali : 10-28-2007 at 09:37 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-29-2007, 03:40 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: To get random number in mysql

Hi Techies,

We can do it in this way also..

-- For getting four digit random number
SELECT FLOOR(1000+ RAND()*8999) FROM DUAL;
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-04-2007, 09:09 PM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: To get random number in mysql

The following code will produce lower case random string length 4

Code:
SET @number:=4;
set @string:='';
SET @sql:=REPLACE('SET @t:=BENCHMARK(X,@string:=concat(@string,char(FLOOR(97+ RAND()*26))));','X',@number);
prepare stm1 from @sql;
execute stm1 ;
select @string;
OUTPUT

Code:
tifg
locm
pord
__________________
-Murali..

Last edited by Murali : 11-05-2007 at 03:45 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-04-2007, 09:12 PM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: To get random number in mysql

The following code will produce UPPER case random string length 4

Code:
SET @number:=4;
set @string:='';
SET @sql:=REPLACE('SET @t:=BENCHMARK(X,@string:=concat(@string,char(FLOOR(65+ RAND()*26))));','X',@number);
prepare stm1 from @sql;
execute stm1 ;
select @string;
OUTPUT

Code:
KTOS
WRET
ETEW
__________________
-Murali..

Last edited by Murali : 11-05-2007 at 03:46 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-05-2007, 05:01 AM
write2ashokkumar write2ashokkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 85
write2ashokkumar is on a distinguished road
Default Re: To get random number in mysql

hi,

how to get the unique random number and random string?
For example, i start the process to insert the data into the table,with the random value, at that time i need the unique value also.... how can we get this?

Regards,
S.Ashokkumar
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-05-2007, 07:38 PM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: To get random number in mysql

Quote:
Originally Posted by write2ashokkumar View Post
hi,

how to get the unique random number and random string?
For example, i start the process to insert the data into the table,with the random value, at that time i need the unique value also.... how can we get this?

Regards,
S.Ashokkumar
we have a function in Mysql UUID() [Universal Unique Identifier].

A UUID is a 128-bit number represented by a string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format:

* The first three numbers are generated from a timestamp.
* The fourth number preserves temporal uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).
* The fifth number is an IEEE 802 node number that provides spatial uniqueness

Code:
mysql> SELECT UUID();
        -> '8dcc1548-dd6d-102a-bf08-0019d123ad2a'
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-05-2007, 07:44 PM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: To get random number in mysql

To get combination of UPPER , lower and number random string length 4

Code:
SET @number:=4;
set @string:='';
SET @sql:=REPLACE('SET @t:=BENCHMARK(X,@string:=concat(@string,CASE char(FLOOR(49+ RAND()*3)) WHEN 1 THEN char(FLOOR(97+ RAND()*26)) WHEN 2 THEN char(FLOOR(65+ RAND()*26)) WHEN 3 THEN char(FLOOR(48+ RAND()*9)) END));','X',@number);
prepare stm1 from @sql;
execute stm1 ;
select @string;
OUTPUT

Code:
Ur4z
E0kb
lpQn
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-05-2007, 10:34 PM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: To get random number in mysql

To get 4 digit random number

Code:
SET @number:=4;
set @string:='';
SET @sql:=REPLACE('SET @t:=BENCHMARK(X,@string:=concat(@string,char(FLOOR(48+RAND()*9))));','X',@number);
prepare stm1 from @sql;
execute stm1 ;
select @string;
OUTPUT
Code:
1484
4532
5234
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create random number in c? Murali C and C++ Programming 6 07-22-2008 11:26 PM
How do I generate a random number from php? itbarota PHP Programming 2 09-25-2007 07:57 AM
How do I generate a random number from php? kingmaker PHP Programming 2 07-24-2007 01:56 AM
MySql Random string priyan Database Support 3 07-13-2007 07:31 AM
Code:PHP Random Functions pranky PHP Programming 0 02-24-2007 12:01 AM


All times are GMT -7. The time now is 12:07 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0