random array index returning values not contained

Thread Tools Search this Thread
Top Forums Programming random array index returning values not contained
# 1  
Old 01-20-2009
random array index returning values not contained

For kicks I wrote up a Password generator after lunch. Let me start with the code:

unsigned int x,y,z,c;

unsigned int KISS();
unsigned int devrand();

int main( int argc, char** argv )
    int i, j = 1;
    char pwd[] = "abcdefghijklmnopqrstuvwxyz"
    char password[12];

    if( argv[1] )
        j = atoi(argv[1]);

    do {
        memset( password, '\0', sizeof(password) );
        for( i = 0; i < sizeof(password); ++i )
            password[i] = (char)pwd[(KISS() % strlen( pwd ))];
        printf("%s\n", password);
    } while( j );

    return 0;
 * Fairly popular Random Number generator
unsigned int KISS()
    x = devrand();
    while(!(y=devrand())); // y must not be zero
    z = devrand();
    c = devrand();

    unsigned long long t;
    unsigned long long a = 698769069ULL;

    x = 69069*x + 12345;

    y ^= (y<<13);
    y ^= (y>>17);
    y ^= (y<<5);

    t = a*z+c;
    c = (t>>32);

    return x+y+(z=t);

 * using a perfectly fine generator to generate a seed >.>"
unsigned int devrand( void )
    int          f;
    unsigned int r;
    f=open("/dev/urandom", O_RDONLY);

    if( f == -1)          exit(-1); // ERROR?
    if( read(f,&r,4)!=4 ) exit(-1); // ERROR?

    return r;

( all there upon request )

Now this works just fine without issues but if I specify my array 'password' to be of say size 9 the program starts returning values not within the 'pwd' array.

Ive had it return quotes brackets and a bunch of various non printables.

Any idea's?

Last edited by VRoemer; 01-20-2009 at 04:57 PM..
# 2  
Old 01-20-2009
Post the entire code. You are missing the code listing for the random number generator KISS_RNG.
# 3  
Old 01-20-2009
I tried just posting what was relevant but give me a sec.

Here is an example crap output with password[9]

You cant see it but there is some control character after the '-'

---- BIG EDIT ----

I think the code is eating the ending NULL Character as The output from 9 is longer than 9 characters which would explain why I get this crappy output


So the fix is subtracting 1 from sizeof( password )

Sorry, I oopsed all over this forum with a rookie mistake.

Last edited by VRoemer; 01-20-2009 at 05:12 PM..
# 4  
Old 01-20-2009
Some points:

You can't place more then 10 chars in the string password:

for( i = 0; i < sizeof(password)-1; ++i )

You must end the string with a NULL character before the printf statement.

password[i+1] = '\0'
printf("%s\n", password);

# 5  
Old 01-20-2009
My memset sets the whole string to NULL for each password we decide to generate so just subtracting from sizeof() should be the end of it right?

But as a best practice Its probably best to do it the way you have said as I'm sure my memset is eating cpu time for no reason.
# 6  
Old 01-20-2009
Better to use what Franklin52 suggested instead of setting all locations to NULL with memset.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy of array by index value fails

Hello, I have a complicated situational find and replace that I wrote in bash because I didn't know how to do everything in awk. The code works but is very slow, as expected. To create my modified file, I am looping through an array that was populated earlier and making some replacements at... (6 Replies)
Discussion started by: LMHmedchem
6 Replies

2. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies

3. Programming

Returning char array

I want to return a char array to the main() function, but its returning garbage value. #include<stdio.h> //#include<conio.h> #include<string.h> char* strtrmm(); int main() { char str1,c1; printf("\n Enter the string:"); gets(str1); //strtrmm(str1); printf("%s",strtrmm(str1));... (2 Replies)
Discussion started by: zinat
2 Replies

4. Shell Programming and Scripting

awk multiply values contained in 2 different files

Hi Everyone ! I have two files with the same configuration and I want to multiply corresponding values and write the result in a file. Let say 2 header lines and then lines of values (with not constant number of columns): more file1.txt --> BLABLABLA BLABLABLA 1 2 3 4 1 2 3 1 2 1... (7 Replies)
Discussion started by: Youm
7 Replies

5. Programming

FORTRAN -Returning index fir which values fall in a region

I have an 10 element array containing numbers, I want the start and end index in the array for which the values lie between DIST1 and DIST2. It is not working quite right. I also might want a value of 0 if I cannot find an index. V=(/10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0/) ... (4 Replies)
Discussion started by: kristinu
4 Replies

6. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

7. Shell Programming and Scripting

awk array index help

$ cat file.txt A|X|20 A|Y|20 A|X|30 A|Z|20 B|X|10 A|Y|40 Summing up $NF based on first 2 fields, $ awk -F "|" 'BEGIN {OFS="|"} { sum += $NF } END { for (f in sum) print f,sum } ' file.txt o/p: A|X|50 A|Y|60 A|Z|20 (4 Replies)
Discussion started by: uwork72
4 Replies

8. UNIX for Advanced & Expert Users

sql variable as array index

hi folks i am facing problom while trying to access sql variable as array index ina unix shell script....script goes as below.. #!/bin/ksh MAX=3 for elem in alpha beeta gaama do arr=$elem ((x=x+1)) Done SQL_SERVER='servername' /apps/sun5/utils/sqsh -S $SQL_SERVER -U user -P pwd -b -h... (1 Reply)
Discussion started by: sudheer157
1 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question

Featured Tech Videos