![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| generate random number in perl | zx1106 | Shell Programming and Scripting | 2 | 03-17-2008 09:13 PM |
| how do i generate random integer using only shell script | sridhusha | Shell Programming and Scripting | 1 | 12-31-2007 12:46 AM |
| Generate a random password | chiru_h | Shell Programming and Scripting | 5 | 10-07-2007 05:03 PM |
| generate random number in korn shell | frustrated1 | Shell Programming and Scripting | 2 | 12-31-2005 04:49 AM |
| How to generate a random number? | MacMonster | High Level Programming | 2 | 10-15-2001 09:35 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Generate Random Password in C
I need a function to generate a random alphanumeric password in C code. It needs to be between 6-8 characters and follow the following rules:
Reject if same char appears # time: 4 or more Reject if same char appears consecutively: 3 or more I have the following random password working for numbers (0-9). I need to update it to include characters (a-z). I also need to add the validation rules above. Can someone please help me. I have been trying for days to update it but get a lot of compile errors... Thanks.. Here's what is working for number (0-9): Random_Password() { int ran_int_num; time_t seconds; time(&seconds); srand ((unsigned int) seconds); /* multiply by 975 to generate a larger number */ ran_int_num = rand() * 975; /**************************************************/ /* Need to cast the integer to character, so use sprintf */ /* %u = unsigned decimal integer %d = signed decimal integer */ /* ************************************************/ sprintf(randompw, "%u", ran_int_num); strncpy(newpw, randompw, 8); return(0); } Here's what I tried to change it to, in order to generate an alphanumeric: GenerateRandomID() { char password_chars["1234567890abcdefghijklmnopqrstuvwxyz"]; int i; int length; char ticketid; int random; for (i = 0; i < length; i++) { random = rand(0, strlen(password_chars) - 1); ticketid = password_chars[random]; } return ticketid; } |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Here is a simple algorithm:
Code:
#include <stdlib.h>
#include <time.h>
static int srand_called=0;
char *random_pw(char *dest)
{
size_t len=0;
char *p=dest;
int three_in_a_row=0;
int arr[128]={0x0};
/* be sure to have called srand exactly one time */
if(!srand_called)
{
srandom(time(NULL));
srand_called=1;
}
*dest=0x0; /* int the destination string*/
for(len=6 + rand()%3; len; len--, p++) /* gen characters */
{
char *q=dest;
*p=(rand()%2)? rand()%26 + 97: rand()%10 + 48;
p[1]=0x0;
arr[*p]++; /* check values */
if(arr[*p]==3)
{
for(q=dest; q[2]>0 && !three_in_a_row; q++)
if(*q==q[1] && q[1]==q[2])
three_in_a_row=1;
}
if(three_in_a_row || arr[*p]> 3 )
return random_pw(dest); /* values do not pass try again */
}
return dest;
}
Last edited by jim mcnamara; 06-11-2008 at 01:59 PM. Reason: change srand to srandom |
|
#3
|
|||
|
|||
|
Generate Random Password in C
Jim, thank you so much. I've been busy on other hot projects, so won't be able to try in for a couple of days. Thanks again for the very quick response.
|
|||
| Google The UNIX and Linux Forums |