Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
1.cannot use random functions
2.cannot use loops
3.output stored in file

What I have tried:

I don't know what to do i didn't try and i don't have idea
Updated 18-Jul-22 5:22am

The first thing you need is a simple algorithm for (pseudo) random number generation. Have a look at Lehmer random number generator - Wikipedia[^].
Share this answer
Patrice T 1-May-22 14:17pm    
CPallini 1-May-22 16:40pm    
Thank you.
jsc42 1-May-22 17:37pm    
That algorithm (plus most others) needs a seed to generate the value. The seed should be randomly chosen, which takes us back to square 1 again unless you have some other pseudo-random number generator to start with (e.g. current time as milliseconds since the epoch mod some large no). Alternatively, see but I doubt that that would be acceptable in the OP's situation.
CPallini 2-May-22 2:14am    
Every pseudo-random generator, I am aware of, needs a seed.
Bibek Das 2-May-22 0:19am    
Can we do this problem with recursion???

int main()
	int *addr, random, lower, upper, choice, count;

	FILE *f1ptr, *f2ptr;

   f1ptr = fopen("a.txt","w");
   f2ptr = fopen("random.txt", "a+");
   if(f1ptr == NULL || f2ptr == NULL)

      printf("\nEnter the lower and upper bound: ");
      scanf("%d %d", &lower, &upper);

      addr = (int *)malloc(sizeof(int));

      fprintf(f1ptr, "%d", addr);

      fscanf(f1ptr, "%d", &random);


      if(random < 0)
         random *= -1;

      random = (random % (upper - lower)) + lower;

      printf("%d\n", random);

      fprintf(f2ptr, "%d", random);

      printf("Press 1 to continue or 0 to exit: ");
      scanf("%d", &choice);

   }while(choice != 0);

	return 0;
Share this answer
Bibek Das 18-Jul-22 11:25am    
But i don't know why it is getting terminated after 2 or 3 steps
jeron1 18-Jul-22 11:37am    
You should really update your original post with this data. In the do-while loop, on the second iteration, you're using f1ptr even though you've previously called fclose(f1ptr).
Patrice T 18-Jul-22 11:39am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 18-Jul-22 11:58am    
This line is incorrect:
      fprintf(f1ptr, "%d", addr);

The variable addr is a pointer, not an integer value, so why not just declare it correctly. You also need to initialise its content with an actual number before writing it to a file.

You also try to write to the file and immediately read from it without closing or rewinding, so whatever you read will not be the content of the file. To be honest all the code connected with the f1ptr file is redundant and should be replaced by calls to the rand function.
Bibek Das 18-Jul-22 12:13pm    
But in my assignment they said we cannot use rand or srand and loop for this program

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900