l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2002 Feb 01 07:48

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] [C newbie]C program is acting weird...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] [C newbie]C program is acting weird...

On Thu, 31 Jan 2002, Ryan wrote:
> for some reason this is spitting out 3 char responses once in a while, anyone 
> have any ideas? I don't know much C, this is just some code I modified.
> Also, if anyone knows how to get it to include numbers mixed in with the 
> letters i'd like to know how.

This is a test message for responses to coding questions to find limits on
the list.

Which of the two response would be better?
Response 1:
Wht not compute the min and max ranges for "length" by plugging in min and
max values for each point and see why you get 3 chars?

Maybe an array of valid chars with other c functions for size would be

Response 2:
Do you know the range of random(), and the effects of the minimum value of
this on your equations for length and allowance for compatability off
ASCII biased systems?

An array of valid chars allows for expandability in the future to allow
for more valid chars and use of strlen (if you array is null
terminated) would make this code more dynamic.

Response 3:

Trying to make a random password generator for your linux system? Maybe
making a keycode random key generator? There are pre-packaged systems to
do the former, while more cryptography and checksums might be better for
the later, but some thoughts on this:

Why not make an array of "valid chars" and then you can make your code
more portable? (To non ASCII systems for example.) (Also, a function call
to this araay with the c string lib for strlen(chr arry for valid
chars) would provide with a new array size for possible charts to choose

Why not seed your random() with something like time()?

Why not just multiply the [0,1) ranged random() value by the max range
allowed for the random number?

You know the max possible size of your string,and since only up to 3 chars
are added, why not look to avoid char* and instead try explicit limits to
a char array?

        /* terminated with null in case string operations are desired, and
            to allow for strlen() computations to work and not count the
            null in the length */ 
        char validChar[] = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0',0];
        int validCharLen = strlen(validChar);

You have: 
>         length = MIN_LEN + random() / (RAND_MAX / (MAX_LEN + 2 - MIN_LEN))-1;

Where is RAND_MAX #defined?

so, random range [0,1), order of operations breaks this down to:
length = (4 + ([0,1) / ( 9 / ?RAND_MAX? / (9 + 2 - 4))) - 1
length = (([0,1) / ( 9 / ?RAND_MAX? / 7 )) + 3
length = (0 / 9 / ? / 7) + 3 = [3,?] (We dont know what RAND_MAX is, so
range limits are made too difficult to address here.)

(You should check what random() does and values it produces. I could be
very wrong here, but it has been a while for me.)

Since random() can be zero (I sem to recall, but am not sure right now),
there is risk for zero being divded by the rest (except the +3) leaving
your length only 3.

The above does not even begin to address your RAND_MAX

/* and for your length= computation, consider */
length = MIN_LEN + ((MAX_LEN - MIN_LEN)*int(random(time())));

/* and for the for loop, what about: */
     string[i] = 'a' + validChar[(int(random(time()))*validCharLen)];

You could change a lot in your code depending upon the intention.

If this is determined to be for a class, I will not be answering any more
questions for you. 

Response 4:
I did not want to go into more detail than the above.

What is considered acceptable onthe list for answers to coding problems?


Version: 3.12
GCS/CM$/IT$/LS$/S/O$ !d--(++) !s !a+++(-----) C++$(++++) U++++$(+$) P+$>+++ 
L+++$(++) E W+++$(+) N+ o K w+$>++>+++ O-@ M+$ V-$>- !PS !PE Y+ !PGP
t@-(++) 5+@ X@ R- tv- b++ DI+++ D+ G--@ e+>++>++++ h(++)>+ r*>? z?
decode: http://www.ebb.org/ungeek/ about: http://www.geekcode.com/geek.html

> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> #include <signal.h>
> #include <stdlib.h>
> #include <time.h>
> #include <string.h>
> #include "version.h"
> #define SESSION_TIMEOUT 20
> #define MAX_RESPONSE 200
> #define MAX_REQUEST 100
> #define MIN_LEN 4
> #define MAX_LEN 9
> char *randusername()
> {
>         char *string;
>         int i, length;
>         // randomly choose a length betweem MIN_LEN and MAX_LEN
>         length = MIN_LEN + random() / (RAND_MAX / (MAX_LEN + 2 - MIN_LEN))-1;
>         if (string = malloc(length + 2), string == NULL)
>                 return NULL;
>         for (i = 0; i < length; i++)
>                 // does your head hurt yet?
>                 string[i] = 'a' + random() / (RAND_MAX / ('z' + 1 - 'a'));
>         // zero terminate!
>         i++;
>         string[i] = '\0';
>         return string;
> }

vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.