Hey,

I was doing some coding for fun, just practicing and seeing if i could still all the sorts and searches. My question is unrelated to that however because i can't seem to figure out an easy way to generate random char arrays.

i found a random() function in the man pages.

long int random(void) which generates a random number between 0 and MAX_RAND.

Does anyone have any ideas on how to get this to give me values between 33-127 ( the printable ASCII values)?

My function so far is

int randomChar()
{
      double rnd;
      double max = MAX_RAND; //i did this to cast the MAX_RAND (int) to a double

      rnd = random();

      return (rnd/max)*127 - (rnd/max)*33;
}
Member Avatar for iamthwee

Create a function where you specify two parameters, min and max.

I'm a novice in C so I can't say if this works but let me know if it doesn't or does.
I'm learning from a book and im just a few chapters before I get to functions but I've done javascripting before so I have an idea of how it works.

int randomChar()
{
      double rnd;
      while ( rnd > 127 && rnd <33)
	  rnd = random();
}

It feels like I'm supposed to have a return value but oh well.

/*
 *    random_ascii.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char *randy( char *ascii, int len )
{
    char *ptr = ascii;
    int i;
    
    for( i = 0; i < len; i++ )
    {
        *( ptr + i ) = rand() % 94 + 33;
    }
    *( ptr + i ) = '\0';
    
    return ptr;    
}

int main( void )
{
    char random_s[20] = { '\0' };
    int i;
    
    srand( (unsigned)time( NULL ) ); 
        
    puts( "\tGive me some random lines\n" );
        
    for( i = 0; i < 10; i++ )
    { 
        randy( random_s, sizeof random_s / sizeof ( char ) );
        printf( "\t%s\n", random_s );
    }
    
    getchar();
    return 0;    
}

Output:

Give me some random lines

        /$[=qeML2Q>qiq,3t@Y&
        3+Nf/)B%FBH<|t6\_[m<
        NWM'?\%z~A$5)t-K*<TK
        l.Spi{p6-~,CZb1h&@qb
        l<2J=d?r~>#TZdlEC)_3
        O_;/ND4EayBi@TvR3bet
        ow'kh^OX>jj"\4{I25|o
        z2QC)=,szJG.Sv]9!I;m
        Ofg~mdF%snDUnwPjuoHL
        ASUR/+W">)kuOi~!fq2z

Oh damn.. About the code I posted before.. It was supposed to be a do..while loop. otherwise its the same.
Can't edit because its been over an hour.

Jaav a couple issues with that code:
1. you have an impossible condition inside that while loop.
2. rnd is initialised after it's use.

yes.. I realised that now.. what about:

int randomChar()
{
int i = 1;
double rnd;

while(i){
rnd = random();
if(rnd < 127 && rnd > 33) i=0;
}}

That should work only thing I'm not sure about is if I should have a return rnd statement at the end and/or parameters.

Ok, you definitely need a return. If you didn't want a return you would say "int main()", but even with main it is advised you do return. I haven't tested your particular segment of code and I don't think I will, but it looks like it would get the desired result. If you want to think about this more, you should think of a more elegant solution (e.g. do you really need another variable int i? etc.) Also, I think "srand" used by Aia is a better solution.

>That should work
No, it won't. Please test your code before posting.

>only thing I'm not sure about is if I should have a return rnd statement at the end

int randomChar() returns a type int. That should give you a clue.

/*
 * r_ascii.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 126
#define MIN 33

int randy( int max, int min )
{
    int r_num;
    do
     {
        r_num = rand();
    }
    while ( r_num < min || r_num > max );
    return r_num;
}

int main( void )
{
    int i;
    
    srand( (unsigned) time( NULL ) );
    
     
     for( i = 0; i < 1000; i++ )
     {
        printf( "%c ", randy( MAX, MIN ) );
    }
    getchar();
    return 0;
}

Output:

b U * = ^ s y F c ; ^ x 5 v - k L 1 0 M r a n c w 4 o 5 w = : ' - 7 ? v ; L $ )
4 ? D I P r d y { D @ e p & O Y C L T % y f U h ( i F , p 8 D [ ^ < p W | V z |
2 : 1 * = 1 n q V & r F X { ] c ) > ] I O ? < < * ! U l W O ' h [ 1 g { x z % I
# R J L ) U : < ( % 9 + 6 S 8 q } , i . 8 O Z ; s D j i y W n o } P z ` i ^ i K
A 2 ] " 5 / = ( - e o c 4 D o g + H O s D 8 ~ ` { ' O Z [ J E 5 x u G z A w Z 6
. P t : ! 4 q f y o b 3 b r ) ~ 0 7 Q * 4 & t _ u M S : 2 u L O = ) b W 8 o H A
: x u e ( a c % K W k R ? _ _ } i A 0 - i ( t ` / 6 # D | 4 t C j ` M \ 8 ^ C V
S T { p < ; \ + 3 c 7 m 5 c P A _ e r l 3 Q | e + " 5 z T D ' S @ i m l e . R _
y u _ ^ ! n O Y $ \ F q { W R ( G 8 6 x _ F c u h $ ~ A < J & y I _ = M J / 6 k
s Z 8 z R { l c m A u T Z ^ 5 Z D g [ J C \ - A | b D 3 2 Q ] q z h # > r o . e
e ~ X ~ t o = 1 % H ) f _ ! * C i V R & J > j h 9 . X o # a ^ W R V > V m y 8 Z
C B D $ K ] ) < x x . n 4 \ z * _ [ o G C o 6 > 1 $ G - Y n v 7 h O V Y 1 * & '
3 M T / f N N " } n ! 5 S t & r + E T W * v _ T v r d [ ` t k B t E ! C G { 8 r
^ 0 l : Y Z g 3 u o q ] s 0 ) v - p j I J w ^ , 5 F - : Z ) \ { H J u " + p I v
^ y 4 r 4 j p \ m t k 9 L h 5 _ " d 3 _ a p l 7 Y p ? V 4 : h ) ) \ < , ? m N "
m a H p Y 1 R t S & T x ~ - ? w J r & w _ ) A u O ` - ~ 2 3 l ! u / $ @ w p W g
j X u K - ] * / & w , e Q U d < J W ` ; i C : V N N 5 2 b x Q x q f % e | F = [
| 5 5 L y c h R ^ 1 l ? N z T , Z 0 L < H ? N [ D . " B f / + H u # l S z w x Y
u x , , & I ~ l _ U ] U m , W . v - h 5 3 s V J < ( A > v ( B * < T g = z Z d w
V H | z ] X ^ < ( o = u M ` 0 n m E { ( 2 ) 3 U k 3 1 M j ` J K u ; 1 Y u { Y j
E ' o q I g Z _ Z y } B Y | K K L m M K b ? p z * g K $ " } # x ) h ~ q / U x U
# ; 9 L a V t C V A c i : D D M ? S r } n ( f k u ; V i M J f k U w $ P # 1 ? B
- f b a M r s 3 S f 9 = . { j ` : L z 5 ? z L # J d ; 7 W [ L . A Q Q % d 8 | w
V ! + d j W N g & t x n 2 [ o Z 4 s 3 E \ _ a u X _ ) G < % [ t 3 9 + y ! W f {
5 - Z J * ^ n N > G [ ` c 5 ` ( F a ! x e Z F ( ' ? K s 9 x ] B > C p G 3 E $ d
/*
 * r_ascii.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 126
#define MIN 33

int randy( int max, int min )
{
    int r_num;
    do
     {
        r_num = rand();
    }
    while ( r_num < min || r_num > max );
    return r_num;
}

int main( void )
{
    int i;
    
    srand( (unsigned) time( NULL ) );
    
     
     for( i = 0; i < 1000; i++ )
     {
        printf( "%c ", randy( MAX, MIN ) );
    }
    getchar();
    return 0;
}

Output:

b U * = ^ s y F c ; ^ x 5 v - k L 1 0 M r a n c w 4 o 5 w = : ' - 7 ? v ; L $ )
4 ? D I P r d y { D @ e p & O Y C L T % y f U h ( i F , p 8 D [ ^ < p W | V z |
2 : 1 * = 1 n q V & r F X { ] c ) > ] I O ? < < * ! U l W O ' h [ 1 g { x z % I
# R J L ) U : < ( % 9 + 6 S 8 q } , i . 8 O Z ; s D j i y W n o } P z ` i ^ i K
A 2 ] " 5 / = ( - e o c 4 D o g + H O s D 8 ~ ` { ' O Z [ J E 5 x u G z A w Z 6
. P t : ! 4 q f y o b 3 b r ) ~ 0 7 Q * 4 & t _ u M S : 2 u L O = ) b W 8 o H A
: x u e ( a c % K W k R ? _ _ } i A 0 - i ( t ` / 6 # D | 4 t C j ` M \ 8 ^ C V
S T { p < ; \ + 3 c 7 m 5 c P A _ e r l 3 Q | e + " 5 z T D ' S @ i m l e . R _
y u _ ^ ! n O Y $ \ F q { W R ( G 8 6 x _ F c u h $ ~ A < J & y I _ = M J / 6 k
s Z 8 z R { l c m A u T Z ^ 5 Z D g [ J C \ - A | b D 3 2 Q ] q z h # > r o . e
e ~ X ~ t o = 1 % H ) f _ ! * C i V R & J > j h 9 . X o # a ^ W R V > V m y 8 Z
C B D $ K ] ) < x x . n 4 \ z * _ [ o G C o 6 > 1 $ G - Y n v 7 h O V Y 1 * & '
3 M T / f N N " } n ! 5 S t & r + E T W * v _ T v r d [ ` t k B t E ! C G { 8 r
^ 0 l : Y Z g 3 u o q ] s 0 ) v - p j I J w ^ , 5 F - : Z ) \ { H J u " + p I v
^ y 4 r 4 j p \ m t k 9 L h 5 _ " d 3 _ a p l 7 Y p ? V 4 : h ) ) \ < , ? m N "
m a H p Y 1 R t S & T x ~ - ? w J r & w _ ) A u O ` - ~ 2 3 l ! u / $ @ w p W g
j X u K - ] * / & w , e Q U d < J W ` ; i C : V N N 5 2 b x Q x q f % e | F = [
| 5 5 L y c h R ^ 1 l ? N z T , Z 0 L < H ? N [ D . " B f / + H u # l S z w x Y
u x , , & I ~ l _ U ] U m , W . v - h 5 3 s V J < ( A > v ( B * < T g = z Z d w
V H | z ] X ^ < ( o = u M ` 0 n m E { ( 2 ) 3 U k 3 1 M j ` J K u ; 1 Y u { Y j
E ' o q I g Z _ Z y } B Y | K K L m M K b ? p z * g K $ " } # x ) h ~ q / U x U
# ; 9 L a V t C V A c i : D D M ? S r } n ( f k u ; V i M J f k U w $ P # 1 ? B
- f b a M r s 3 S f 9 = . { j ` : L z 5 ? z L # J d ; 7 W [ L . A Q Q % d 8 | w
V ! + d j W N g & t x n 2 [ o Z 4 s 3 E \ _ a u X _ ) G < % [ t 3 9 + y ! W f {
5 - Z J * ^ n N > G [ ` c 5 ` ( F a ! x e Z F ( ' ? K s 9 x ] B > C p G 3 E $ d

i think for upper limit u can use a mod funtion ... i.e r_num =r_num % 127 ... it will save a lot of comparisons ... beacuse it will convert any number above 127 within the range of 127.

to get a random within a range

int min = 33;
int max = 127;
int range = (max - min) + 1;
 
//now generate some random numbers
int somerandomnum = min + (int)(range * rand() / (RAND_MAX + 1.0));
 
//method will always generate a random number within range
/*
 * r_ascii.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define MAX 126
#define MIN 33
 
int randy( int max, int min )
{
    int r_num;
    do
     {
        r_num = rand();
    }
    while ( r_num < min || r_num > max );
    return r_num;
}
 
int main( void )
{
    int i;
 
    srand( (unsigned) time( NULL ) );
 
 
     for( i = 0; i < 1000; i++ )
     {
        printf( "%c ", randy( MAX, MIN ) );
    }
    getchar();
    return 0;
}

Output:

b U * = ^ s y F c ; ^ x 5 v - k L 1 0 M r a n c w 4 o 5 w = : ' - 7 ? v ; L $ )
4 ? D I P r d y { D @ e p & O Y C L T % y f U h ( i F , p 8 D [ ^ < p W | V z |
2 : 1 * = 1 n q V & r F X { ] c ) > ] I O ? < < * ! U l W O ' h [ 1 g { x z % I
# R J L ) U : < ( % 9 + 6 S 8 q } , i . 8 O Z ; s D j i y W n o } P z ` i ^ i K
A 2 ] " 5 / = ( - e o c 4 D o g + H O s D 8 ~ ` { ' O Z [ J E 5 x u G z A w Z 6
. P t : ! 4 q f y o b 3 b r ) ~ 0 7 Q * 4 & t _ u M S : 2 u L O = ) b W 8 o H A
: x u e ( a c % K W k R ? _ _ } i A 0 - i ( t ` / 6 # D | 4 t C j ` M \ 8 ^ C V
S T { p < ; \ + 3 c 7 m 5 c P A _ e r l 3 Q | e + " 5 z T D ' S @ i m l e . R _
y u _ ^ ! n O Y $ \ F q { W R ( G 8 6 x _ F c u h $ ~ A < J & y I _ = M J / 6 k
s Z 8 z R { l c m A u T Z ^ 5 Z D g [ J C \ - A | b D 3 2 Q ] q z h # > r o . e
e ~ X ~ t o = 1 % H ) f _ ! * C i V R & J > j h 9 . X o # a ^ W R V > V m y 8 Z
C B D $ K ] ) < x x . n 4 \ z * _ [ o G C o 6 > 1 $ G - Y n v 7 h O V Y 1 * & '
3 M T / f N N " } n ! 5 S t & r + E T W * v _ T v r d [ ` t k B t E ! C G { 8 r
^ 0 l : Y Z g 3 u o q ] s 0 ) v - p j I J w ^ , 5 F - : Z ) \ { H J u " + p I v
^ y 4 r 4 j p \ m t k 9 L h 5 _ " d 3 _ a p l 7 Y p ? V 4 : h ) ) \ < , ? m N "
m a H p Y 1 R t S & T x ~ - ? w J r & w _ ) A u O ` - ~ 2 3 l ! u / $ @ w p W g
j X u K - ] * / & w , e Q U d < J W ` ; i C : V N N 5 2 b x Q x q f % e | F = [
| 5 5 L y c h R ^ 1 l ? N z T , Z 0 L < H ? N [ D . " B f / + H u # l S z w x Y
u x , , & I ~ l _ U ] U m , W . v - h 5 3 s V J < ( A > v ( B * < T g = z Z d w
V H | z ] X ^ < ( o = u M ` 0 n m E { ( 2 ) 3 U k 3 1 M j ` J K u ; 1 Y u { Y j
E ' o q I g Z _ Z y } B Y | K K L m M K b ? p z * g K $ " } # x ) h ~ q / U x U
# ; 9 L a V t C V A c i : D D M ? S r } n ( f k u ; V i M J f k U w $ P # 1 ? B
- f b a M r s 3 S f 9 = . { j ` : L z 5 ? z L # J d ; 7 W [ L . A Q Q % d 8 | w
V ! + d j W N g & t x n 2 [ o Z 4 s 3 E \ _ a u X _ ) G < % [ t 3 9 + y ! W f {
5 - Z J * ^ n N > G [ ` c 5 ` ( F a ! x e Z F ( ' ? K s 9 x ] B > C p G 3 E $ d

Some comments might help the beginners Aia

>Some comments might help the beginners Aia
Read the code with a C reference manual, step through it, and if you still have questions, ask them.

>Some comments might help the beginners Aia
Read the code with a C reference manual, step through it, and if you still have questions, ask them.

I understand the code, it was for the C beginners trying to understand the code (as there were 0 comments in his code)

>I understand the code
Good for you.

>it was for the C beginners trying to understand the code
Oddly enough, so was my suggestion.

>(as there were 0 comments in his code)
The only comment I would add is 1 at the top describing what the program prints. Beyond that the code is trivial and doesn't need comments.

Hey thanks a lot guys, now i just need to work on a recursive merge sort.

peace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.