I want to generate 1000 random number from 65 to 122 but exclude 91-96 by using rand() and srand(1). How can I do it ? These random number I just want the ASCII codes of the letters in the alphabet.
I want to generate 1000 random number from 65 to 122 but exclude 91-96 by using rand() and srand(1). How can I do it ? These random number I just want the ASCII codes of the letters in the alphabet.
Well this is how I would do it. You only need to call rand() once per character, and there's no need to create an array containing both the upper and lower case alphabets. The generate_random_letters() function returns a string, so I added a null byte at the end so you can print it without crashing:
#include <stdio.h>
#include <stdlib.h>
char *generate_random_letters(int length) {
int i, r;
char *p, *s;
p = s = malloc(length+1);
for (i=0; i<length; i++) {
r = rand() % 52 + 'A';
if (r > 'Z') r += 'a' - 'Z' - 1;
*p++ = r;
}
*p = '\0';
return s;
}
int main() {
char *str;
srand(1);
str = generate_random_letters(1000);
puts(str);
free(str);
return 0;
}
You want to create an array of 1000 characters which includes both upper-case and lower-case letters. See ASCII Table and Description. In order to do so, you must create a random value in that range and then check if it is within the range of excluded characters before considering it a good character and incrementing the array index to repeat for the next, e.g.
#define NRAND 1000 /* if you need a constant, #define one (or more) */
...
size_t nrand = 0; /* number of values generated */
...
do { /* loop */
rval[nrand] = rand() % ('z' - 'A' + 1) + 'A'; /* get rand value in range */
/* if the value isn't in the inner exclusion range */
if (rval[nrand] <= 'Z' || 'a' <= rval[nrand])
nrand++; /* increment counter */
} while (nrand < NRAND); /* while less than 1000 numbers generated */
To compute the random number in range, you must subtract the lower limit of the rage so it is zero based and add 1 to accommodate the full range. You generate your random modulo the reduced max of range + 1 and then add the minimum of the range back to the random value to arrive a a value between 'A' and 'z'. Then you simply check if the value is below the lowest exclusion minimum or above the highest exclusion max. If so, you have a good value, increment your index counter and keep going. If your generated value falls within the exclusion range, the index is left untouched and you simply try again with the next generated value.
Putting it altogether you could do something like:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NRAND 1000 /* if you need a constant, #define one (or more) */
int main (void) {
int rval[NRAND] = {0}; /* array to hold 1000 values */
size_t nrand = 0; /* number of values generated */
srand (time(NULL)); /* seed random number generator */
do { /* loop */
rval[nrand] = rand() % ('z' - 'A' + 1) + 'A'; /* get rand value in range */
/* if the value isn't in the inner exclusion range */
if (rval[nrand] <= 'Z' || 'a' <= rval[nrand])
nrand++; /* increment counter */
} while (nrand < NRAND); /* while less than 1000 numbers generated */
for (int i = 0; i < NRAND; i++) { /* output results in 25 x 40 grid */
if (i && i % 40 == 0)
putchar ('\n');
printf (" %c", rval[i]);
}
putchar ('\n');
}
Example Use/Output
Outputting in 25 rows of 40 characters, you would have:
$ ./bin/randchars
B m m d D I l G w L n C u O M w e c e o i T t K E y q f k r X V T E n d s y P m
t V y p z Y U M C p x F l v v W m n t x F O B t j l r q V c L T X D l d p T n d
E a W S w A F h y L v r w f d f y O K c n z y K G y O g U Z U W I S Z w C V b f
B u H R t R s e q C k o I O A Z j V q k P g b f F E r Y f J v H u E m a R g Q y
h y G O L w q d E K j b H F Z k c S j P C H p U f G K S I R d M F p J w z k W y
h p V P P a z d p t k F O P a x Y j b e h t R i p Q Z L H G s O H C N l j y T M
E b T A d b v F O o W x O g M X A X v A e T i c n F n n b X U U I l Q Q A w c Y
o D h j h M q s m Y f r E Q Q e p y e a O G q e G e M W R H v A c c E Y r j y a
n P r G m T n c q U i w C S u h W J y E q y v O s e p q K F t a j F U R v a z r
c J d H a e x w j o u G b A y D q o F I m z b w n f r T a n J v E o s A V m x m
Z A F G d j x K D t x i c I C I v a J q e p b K F O v t o v L m t o i q b n E B
n H J j T K A y L w f a r W N e b z O M E z g k D h C r M c C e e C c A F W f N
B l k c O j C S n t y p t r N F R H b v B C w H Y Z T G O o i U c g I J B X s G
K N w x O y l N O L k m W q s K W M z l T G D H U N p W S l U a b x o p A W c W
F S Y p I V n O Y H V l Y k e J E K c u q X u N r R S K b n b X G j m t x C B K
n V L F G M C Q o w H E q M n z x b J y P F c B C e u M Q H i E o a G x M u w Q
d I E c t B Q z p V Z i Q E U b D h f j N e g c z c c y Q d a L n m l D q L Y C
e c P K p l r H I R c H m G j c O L G o J c I u z F v t b W r Z J I I c R k k D
j R x U e m k g M t a b s d F l F o K G P S O j e z Z a l D F a H q M L b e n Z
A I u G t r m E x t O E d k D C Q d f T y d l G R x R s w J E D Y y n L u k E A
o h l j k B K N M J q q H H f Q z F N A a R E y H r B t Z G m G f p V W s t R Q
A R w Q Q t J R h N I g w J Y N H K T n h B A v l V e n e E m M L I V p f T O g
l o v h b i b V Z D d v q H m Z K q e S M H z e S f R w H F j g g K N N X I A q
z r c Y B Z D T K S t p y Z D I K j K X o v x S N n S F O k G n S u q e n z K Y
h Z j F y l v m i O z U a E Q D r w V l m r W P r a c H G G a p D u g o p Y V D
You can do a quick check that both 'A' and 'Z' are present as well as 'a' and 'z' (and no funny non-characters)
Look things over and let me know if you have further questions.
I start by the supposition that you have a rand() function, generating a random floating point number between 0 and 1 (the maximum value never getting generated).
This supposition seems to be wrong: the function rand() generates an integer number between 0 and RAND_MAX inclusive. Well, in that case, let's write a new function small_rand(), which generates a random number between 0 and 1, as follows:
double small_rand() {
return (rand()) / ((double)RAND_MAX + 1); // the typecast is needed to avoid integer division
}
You then start by generating a random number between 0 and 51 (there are 52 in total, the double of 26, being the amount of numbers in the alphabet). This can be done, either using 52 as an input parameter, either by multiplying the standard random number with 52:
int random_number = (int) (small_rand() * 52);
Once you have this, you make the distinction: is the number in the range 0-25, then you choose the range between 65 and 91. Is the number in the range 26-51, then you choose the range between 97 and 122:
if (random_number < 26)
result = random_number + 65
else
result = random_number - 26 + 97;
For a completely portable solution, you can simply generate random numbers in the range 0..51 and use them as an index into the set of allowed characters:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NRAND 1000
int main() {
int array[NRAND];
srand(1); /* if you are requested to initialize the random number generator
with a specific value so the output is predictable. Otherwise
you could use srand(time(NULL)); */
for (int i = 0; i < NAND; i++) {
array[i] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 52];
}
for (int i = 0; i < NRAND; i++) {
putchar(array[i]);
putchar(" \n"[i % 40 == 39]; // arrange the output in rows of 40 elements
}
return 0;
}