Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

rand in c with example

+1 vote
asked Mar 14, 2019 by naveen kumar (150 points)

1 Answer

0 votes
answered Mar 15, 2019 by Ryan (1,230 points)

This is basically how you would do it. For rand(), whatever number you put after the plus sign is what the lowest number it will ever give you. The number before the modulus operator is the highest number it will ever give you.


#include <time.h>

#include <stdlib.h>

int main()

{

    srand(static_cast<unsigned int>(time(0)); // Seed the random number generator

    int randomNum = rand() % 10 + 1; // Random number between 1 and 10 

}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...