Sponsored Content
Top Forums Programming random array index returning values not contained Post 302278557 by VRoemer on Tuesday 20th of January 2009 02:58:11 PM
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:

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"
                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                 "1234567890"
                 "!@#$";
    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);
        --j;
    } 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?

    close(f);
    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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
randomword(3)						     Library Functions Manual						     randomword(3)

NAME
randomword, randomchars, randomletters - Generate random passwords (Enhanced Security) LIBRARY
Security Library - libsecurity.so SYNOPSIS
int randomchars( char *string, unsigned short int minlen, unsigned short int maxlen, boolean restrict, long seed); int randomletters( char *string, unsigned short int minlen, unsigned short int maxlen, boolean restrict, long seed); int randomword( char *word, char *hyphenated_word, unsigned short int minlen, unsigned short int maxlen, boolean restrict, long seed); PARAMETERS
Points at a user-supplied space to contain a null-terminated password. Specifies the minimum length that a generated word can have. Spec- ifies the maximum length that a generated word can have. Specifies whether restrictions are to be imposed on the generated word. This is a boolean integer, where a non-zero integer indications restrictions and a 0 (zero) indicates no restrictions. Specifies an initial seed for the random number generator. Points at a user-supplied space to contain a null-terminated random pronounceable password. Contains the hyphenated version of the generated word. DESCRIPTION
These functions generate random passwords for use in password selection. All of them are generated by the system, based on seeds and set in the function. Such seeds can be created with the drand48(), rand(), or random() functions. The randomchars() function places a null-terminated password composed of random printable ASCII characters into the string parameter and returns the length of the generated string. The minlen parameter can equal maxlen, but cannot be greater than maxlen, and cannot be nega- tive. The user space preallocated is at least maxlen for string. The smaller minlen and maxlen are, the smaller the selection space of random words. The restrict parameter is 0 (zero) when no restrictions are placed on the generated word. It is nonzero when the words generated pass the tests of the acceptable_password() function. The seed parameter is used by the function only on the first time it is called; the parameter is ignored on subsequent calls. The randomletters() function places a null-terminated password composed of random lower-case letters into the string parameter and returns the length of the generated word. The minlen, maxlen, restrict, and seed parameters are the same as for the randomchars() function. The randomword() function places a null-terminated random pronounceable password into the word parameter and returns the length of the gen- erated word. The minlen, maxlen, restrict, and seed parameters are the same as for the randomchars() and randomletters() functions. The user space preallocated is at least 2*max - 1 for hyphenated_word. NOTES
The password generator relies on a random number generator that produces uniformly distributed integers. Because the password generator invokes the random number generator many times even for one word, the random number generator has to produce a uniform distribution. The period (distinct numbers produced given a particular seed) and number space (range of possible numbers) must both be large. The drand48() functions are used for this purpose. Programs using these functions must be compiled with -lsecurity. FILES
System password file. System group file. RELATED INFORMATION
Functions: acceptable_password(3), drand48(3), rand(3), random(3). Commands: login(1), passwd(1). delim off randomword(3)
All times are GMT -4. The time now is 08:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy