I am making a library management in C for practice. Now, in studentEntry I need to generate a long int studentID in which every digit is non-zero. So, I am using this function:
long int generateStudentID(){
srand(time(NULL));
long int n = 0;
do
{
n = rand() % 10;
}while(n == 0);
int i;
for(i = 1; i < 10; i++)
{
n *= 10;
n += rand() % 10;
}
if(n < 0)
n = n * (-1); //StudentID will be positive
return n;
}
output
Name : khushit
phone No. : 987546321
active : 1
login : 0
StudentID : 2038393052
Wanted to add another student?(y/n)
I wanted to remove all zeros from it. Moreover, when I run the program the first time the random number will be the same as above, and second time random number is same as past runs like e.g:-
program run 1
StudentID : 2038393052
StudentID : 3436731238
program run 2
StudentID : 2038393052
StudentID : 3436731238
What do I need to fix these problems?
long int temp = 0; while(n != 0){ int m = n % 10 if(m == 0){ m =1; } temp = temp * 10 +m; n/=n; } n = 0; // reverse one more time to make the no as it was while(temp !=0 ){ int m = temp % 10 if(m == 0){ m =1; } n = n * 10 +m; temp/=temp; } return n;but where there is zero it is fixed that i will have one. – Khushit Shah Apr 17 '18 at 03:33