There are some problems in your C code, coming from a different language:
- use
return
to return a value from a function and specify the return type before the function name`.
- our attempt at formating the number is incorrect and unneeded. Incidentally,
RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1);
uses the operator ,
that ignores its left operand and evaluates to the right operand, whose value is stored into a local variable with automatic storage duration, and is lost upon function exit. Just use return a * 1000 + b * 100 + c * 10 + d * 1;
.
- to compute a 4 digit pseudo-random value, you could just use
rand() % 10000
, but an approach such as your may be needed for more digits or for specific constraints such as unique digits.
- to make your results more random, use a faster changing source for
srand()
such as clock()
and initialize the pseudo-random number generator just once int the main()
function.
- you could use the
printf
format "%04d"
to make display an initial 0
if the result is less than 1000.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d;
a = rand() % 10;
b = rand() % 10;
c = rand() % 10;
d = rand() % 10;
while (b == a) {
b = rand() % 10;
}
while (c == b || c == a) {
c = rand() % 10;
}
while (d == c || d == b || d == a) {
d = rand() % 10;
}
return a * 1000 + b * 100 + c * 10 + d;
}
int main() {
int x;
srand(clock());
x = createNumb();
printf("%04d\n", x);
return 0;
}
Note that you can avoid the loops in creatNumb()
. Here is a modified version that takes an argument from the command line to produce multiple results.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
return a * 1000 + (b + bb) * 100 + (c + cc) * 10 + (d + dd);
}
int main(int argc, char *argv[]) {
int x, n;
srand(clock());
n = (argc > 1) ? strtol(argv[1], NULL, 0) : 1;
while (n-- > 0) {
x = createNumb();
printf("%04d\n", x);
}
return 0;
}
Boolean operators evaluate to 1
when they are true and 0
otherwise. bb = (b >= a);
is a short notation for:
bb = 0;
if (b >= a)
bb = 1;
If you want to compute an array of 4 numbers with the given constraint, you should pass a pointer to the destination array as an argument to creatNumb()
. Here is a modified version with this approach:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void createNumb(int *dest) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
dest[0] = a;
dest[1] = b + bb;
dest[2] = c + cc;
dest[3] = d + dd;
}
int main(int argc, char *argv[]) {
int array[4];
srand(clock());
createNumb(array);
printf("%d%d%d%d\n", array[0], array[1], array[2], array[3]);
return 0;
}
Note that passing an array as a argument to a function actually passes a pointer to its first element: createNumb(array);
is equivalent to createNumb(&array[0]);
. This process is referred to as arrays decay into pointers in most expression contexts, it is somewhat confusing for beginners but allows for efficient argument passing.