Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write statements that assign random integers to the variable n in the following ranges:
a) 1 ≤ n ≤ 6

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>
int main() {
  int n;

  printf("random number in [1,100]\n");

 
    n = rand() % 100 + 1;
    printf("%d\n", n);

  return 0;
}
Posted
Updated 5-Apr-22 20:31pm
v2
Comments
jeron1 5-Apr-22 11:38am    
You have not asked a question. Just an observation, one would generally call srand(...) before rand(). What are you attempting to do with the '% 100' portion of your rand() statement?

1 solution

Read the instructions again. You need to create numbers in the range [1..6], not [1..100].
 
Share this answer
 
v2
Comments
CPallini 6-Apr-22 2:32am    
5.OP code generates a random number in the [1..100] range.
Richard MacCutchan 6-Apr-22 3:16am    
Thanks, corrected.

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