Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating a 12Digit Alphanumeric password 2

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I need something that'll create a quick alphanumeric(a-&gt;z, A-&gt;Z, 1-&gt;0) 12 digit character array<br>
something i can just throw in a char something[13]; <p>Karl<br><a href=mailto:kb244@bellsouth.net>kb244@bellsouth.net</a><br><a href= </a><br>
 
Ok i looked into it, got something made, tho microsoft MSDN was no help at all, and i do remeber there used to be a random command where you just stick in the ranges, but M$ had no info on this instead i came up with this lil example<br>
<br>
#include &lt;iostream.h&gt;<br>
#include &lt;stdlib.h&gt;<br>
#include &lt;stdio.h&gt;<br>
#include &lt;time.h&gt;<br>
<br>
void main( void )<br>
{<br>
int i,j; char tmp[13];<br>
i=j=0;<br>
srand( (unsigned)time( NULL ) );<br>
for( i = 0; i &lt;= 12;i++ )<br>
{<br>
<br>
while((j&lt;48) ¦¦ ((j&gt;57)&&(j&lt;65)) ¦¦ ((j&gt;90)&&(j&lt;97)) ¦¦ (j&gt;122))<br>
{<br>
j = rand();<br>
}<br>
tmp<i> = j;<br>
cout&lt;&lt;j&lt;&lt;' ';<br>
j=0;<br>
}<br>
tmp[13]='\0';<br>
cout&lt;&lt;'\n'&lt;&lt;tmp;<br>
}<br>
/*<br>
48-&gt;57 numbers<br>
65-&gt;90 capital letters<br>
97-&gt;122 lowercase letters<br>
*/<br>
<p>Karl<br><a href=mailto:kb244@bellsouth.net>kb244@bellsouth.net</a><br><a href= </a><br>
 
Hello karl, i couldn´t compile your source code with my Microsoft VC++&nbsp;&nbsp;compile, I had to make some changes,<br>first of all remember that an n element array in C are numerated from 0 to n-1, and you have to asign&nbsp;&nbsp;a value to each element of this array, here comes my codes with some comments.<br>It was beautiful to see your program.<br><br><br>#include &lt;iostream.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;time.h&gt;<br>#include &lt;string.h&gt;<br><br>void main( void )<br>{<br>int i,j,nCount; char tmp[13];<br>i=j=0;<br>srand( (unsigned)time( NULL ) );<br>for( i = 0; i &lt; 12;i++ )<br>{<br>while((j&lt;48) ¦¦ ((j&gt;57)&&(j&lt;65)) ¦¦ ((j&gt;90)&&(j&lt;97)) ¦¦ (j&gt;122))<br>{<br>j = rand();<br>}<br>tmp<i> =&nbsp;&nbsp;&nbsp;j;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//You have to asign each character individually<br>cout&lt;&lt;j&lt;&lt;' ';<br>j=0;<br>}<br><br>tmp [12]='\0';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//The last element of your string is tmp [12]<br>nCount=strlen(tmp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//with it I count the lenght of the string<br>cout&lt;&lt;'\n'&lt;&lt;tmp&lt;&lt;'\n';<br>cout &lt;&lt;&quot;Lenght of the string is &quot;&lt;&lt; nCount&lt;&lt;'\n';<br>}<br>/*<br>48-&gt;57 numbers<br>65-&gt;90 capital letters<br>97-&gt;122 lowercase letters<br>*/<br><br><br>Javi
 
Karl,<br><br>To generate a number between 48 and 122 do this:<br><br>int num = (rand() % (122 + 48)) + 48;<br><br>You will still have to check for the gaps<br><br>Good luck<br>-pete
 
this is an extremly old topic I forgot about, and Javi I know its zero-based(your implementation is nice too), and palbano, I appreciate that implementation(the project is long old that used that), it will at least help others if they need an random alphanumeric generator. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Here's some odd tid bits<br>Alpha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;are 0x40 for 26 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and 0x60 for 26<br>Numerics are 0x30 for 10<br><br>Notice Alpha case shifts by exclusive ORing a hex 20 (0x20)<br><br>Using this information <br><FONT FACE=monospace> <br>int d(int x) { // return random number between 0 and x-1<br>&nbsp;&nbsp;return (rand() % ((2&gt;x), 2, x));&nbsp;&nbsp;// Don't let bad input screw things<br>}<br>char c() { // Return random alphanumeric<br>&nbsp;&nbsp;&nbsp;if (d(2)) return (0x30 + d(10));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Numeric this time.<br><br>&nbsp;&nbsp;&nbsp;return (0x40 + d(26) + (d(2) * 0x20));&nbsp;&nbsp;&nbsp;// Numeric fell thru go alpha.<br>}<br></font><br>This allows : <FONT FACE=monospace>for( i =0; i &lt; (sizeof(pwd)-1); i++)&nbsp;&nbsp;&nbsp;pwd[ i ]&nbsp;&nbsp;= c();</font><br><br><br>One could eliminate the d(x) function at the cost of code readability.&nbsp;&nbsp;&nbsp;Even with that, the maintainability of this solution is outweighed by the performance hit.&nbsp;&nbsp;<br><br>A solution that addresses both performance and maintenance uses an array of char containing all your valid characters in a random sequence and a random index to pick a character to return.<br><FONT FACE=monospace><br>char c() {<br>char csMyChars[] = &quot;thelist of chars...&quot;;<br>return csMychars[rand() % (sizeof(csMyChars)-1)]; <br>} // or something like that.<br></font><br>Again allowing main stream code such as : <FONT FACE=monospace>for( i =0; i &lt; (sizeof(pwd)-1); i++)&nbsp;&nbsp;&nbsp;pwd[ i ]&nbsp;&nbsp;= c();<br></font><br>This reduces your overhead dramatically as it eliminates the multiple random number generator calls and virtually eliminates the need for any logic at all.&nbsp;&nbsp;The offsetting cost is a little space in the data section and a visible literal in your executable.&nbsp;&nbsp;It does have the added benifit of allowing you to tune your base set and give more or less weight to certain outcomes.&nbsp;&nbsp;If you want to give numerics a higher probability of inclusion, simply increase their presence in the base array.&nbsp;&nbsp;Likewise if you want to totally eliminate the possibility of a value, simply don't include it in the base set.<br><br>Since I hate involved logic when other simpler methods exist I prefer the last solution.&nbsp;&nbsp;Besides providing the most versitility, it is the fastest executing and easiest to maintain version.&nbsp;&nbsp;Still, nothing I've seen so far is off your path.&nbsp;&nbsp;<br><br>In the words of a very wise person: &quot;Less is more.&quot;<br>Hope this helps, <p>Wil Mead<br><a href=mailto:wmead@optonline.net>wmead@optonline.net</a><br><a href= > </a><br>
 
Pete,<br><br>Perhaps<br><FONT FACE=monospace>int num = (rand() % (122 - 48)+1) + 48;</font><br>would work much better.<br><br>Algorithm:<br>&nbsp;&nbsp;&nbsp;generate random number 0 to size-of-range -1, or modulo size-of-range.<br>&nbsp;&nbsp;&nbsp;Add Base offset to generated number.<br><br>&nbsp;&nbsp;&nbsp;size-of-range = (max - min) +1<br><br>Another example, generate random between 4 and 5<FONT FACE=monospace><br>&nbsp;(5-4) + 1 is 2<br>&nbsp;rand() % 2 is 0 or 1 <br>&nbsp;Add that to 4 yielding 4 or 5.<br></font> <p>Wil Mead<br><a href=mailto:wmead@optonline.net>wmead@optonline.net</a><br><a href= > </a><br>
 
Oops -- fudged the alpha start points... They are 0x61 and 0x41. <p>Wil Mead<br><a href=mailto:wmead@optonline.net>wmead@optonline.net</a><br><a href= > </a><br>
 
Wil,<br><br>I'm sure you are correct. I don't concern myself with 'off by one' math errors in example code. That is left to the developer as a final task. But I appreciate your thoroughness... is that a word? ;o)<br><br>-pete
 
Actually that was only to draw your attention to the mis-coded triadic.<br>return (rand() % ((2&gt;x), 2, x));<br>should be<br>return (rand() % ((2&gt;x) ? 2 : x)); <p>Wil Mead<br><a href=mailto:wmead@optonline.net>wmead@optonline.net</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top