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
RAND(3) 						     Linux Programmer's Manual							   RAND(3)

NAME
rand, rand_r, srand - pseudo-random number generator SYNOPSIS
#include <stdlib.h> int rand(void); int rand_r(unsigned int *seedp); void srand(unsigned int seed); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): rand_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE DESCRIPTION
The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]). The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the rand() function is automatically seeded with a value of 1. The function rand() is not reentrant or thread-safe, since it uses hidden state that is modified on each call. This might just be the seed value to be used by the next call, or it might be something more elaborate. In order to get reproducible behavior in a threaded applica- tion, this state must be made explicit; this can be done using the reentrant function rand_r(). Like rand(), rand_r() returns a pseudo-random integer in the range [0, RAND_MAX]. The seedp argument is a pointer to an unsigned int that is used to store state between calls. If rand_r() is called with the same initial value for the integer pointed to by seedp, and that value is not modified between calls, then the same pseudo-random sequence will result. The value pointed to by the seedp argument of rand_r() provides only a very small amount of state, so this function will be a weak pseudo- random generator. Try drand48_r(3) instead. RETURN VALUE
The rand() and rand_r() functions return a value between 0 and RAND_MAX (inclusive). The srand() function returns no value. CONFORMING TO
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001. The function rand_r() is from POSIX.1-2001. POSIX.1-2008 marks rand_r() as obsolete. NOTES
The versions of rand() and srand() in the Linux C Library use the same random number generator as random(3) and srandom(3), so the lower- order bits should be as random as the higher-order bits. However, on older rand() implementations, and on current implementations on dif- ferent systems, the lower-order bits are much less random than the higher-order bits. Do not use this function in applications intended to be portable when good randomness is needed. (Use random(3) instead.) EXAMPLE
POSIX.1-2001 gives the following example of an implementation of rand() and srand(), possibly useful when one needs the same sequence on two different machines. static unsigned long next = 1; /* RAND_MAX assumed to be 32767 */ int myrand(void) { next = next * 1103515245 + 12345; return((unsigned)(next/65536) % 32768); } void mysrand(unsigned seed) { next = seed; } The following program can be used to display the pseudo-random sequence produced by rand() when given a particular seed. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { int j, r, nloops; unsigned int seed; if (argc != 3) { fprintf(stderr, "Usage: %s <seed> <nloops> ", argv[0]); exit(EXIT_FAILURE); } seed = atoi(argv[1]); nloops = atoi(argv[2]); srand(seed); for (j = 0; j < nloops; j++) { r = rand(); printf("%d ", r); } exit(EXIT_SUCCESS); } SEE ALSO
drand48(3), random(3) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2010-10-01 RAND(3)
All times are GMT -4. The time now is 02:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy