Rand special numbers in C [on hold]


I want to write a function that will generate random numbers.
But the problem is that I don’t know how to generate numbers with a specific criterion, which is: The numbers that I need must be only with 4 digits, with digits between 1 to 6, and there is will not be double digits.

For example:

Acceptable: 1234, 5312, 3425, 3124, etc.

Not acceptable: 234, 1223, 1238, etc.

I know only this way to make random numbers:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
    int randNum = 0;
    srand (time(NULL));

    randNum = rand() %1235;
    printf("%d\n", randNum);

    return 0;

Constraint: I am relatively new to C programming and I am not comfortable with using pointers due to lack of understanding.

I hope that you can help me.


Generate four numbers between 0 and 5 and do a bit of math to join them up.
So you want us to plan, design and implement your homework for you?
@Mat Don’t forget to do some extra bookkeeping to get rid of dupes
Use an char array as char digits[] = "123456"; then shuffle the array. Then use the first four characters.
Hum yep, the shuffle is a great idea.



Just generate some random number less than 10000. Then check that it verifies your additional criteria. If it does, print it. Or generate four random digits less than 6, and check that they form something satisfactory.

You might want to convert that number to some short string. Consider using snprintf.

You could also construct the set of all possible such numbers, and draw randomly an element from it.

You probably need some loop e.g. a do{} while()


In this case the probability of acceptance is around 5%. I’d consider drawing a random element of an array containing all the possibilities.