Skip to content

Instantly share code, notes, and snippets.

@lars-erik
Created April 30, 2012 11:30
Show Gist options
  • Save lars-erik/2557489 to your computer and use it in GitHub Desktop.
Save lars-erik/2557489 to your computer and use it in GitHub Desktop.
Random Password Generators
using System;
public class RandomPassword
{
private static Random random = new Random();
public static string Create(int length = 8, float charRatio = .8f)
{
string password = "";
for(int i = 0; i<length; i++)
{
if (random.NextDouble() < charRatio)
password += Char.ConvertFromUtf32(97 + random.Next(0, 26));
else
password += Char.ConvertFromUtf32(49 + random.Next(0, 9));
}
return password;
}
}
if exists (select * from sys.objects where object_id = OBJECT_ID(N'[dbo].[spGetRandomPassword]') and type in (N'p', N'pc'))
drop procedure [dbo].[spGetRandomPassword]
go
create proc dbo.spGetRandomPassword(
@password varchar(255) out,
@max int = 8,
@charRatio float = 0.8
)
as
begin
declare
@charOrNum float,
@char char,
@counter int
select
@counter = 0,
@password = ''
while @counter < @max
begin
select @charOrNum = RAND()
if @charOrNum < @charRatio
select @char = char(97+cast(RAND()*26 as int))
else
select @char = char(49+cast(RAND()*9 as int))
select
@password = @password + @char,
@counter = @counter + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment