Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

random number c

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

int main ()
{
   int card;

   /* Do this only once at the start of your program,
      otherwise your sequence of numbers (game) will
      always be the same! */
   
   srand(time(NULL));

   /* Choose a number between 1 and 10 - we've called this 'card'
      because the number represents a card from ace to a ten in this
      program (rand() produces a large random int, we find the
      remainder from diving by 10 using '%' mod operator, then add 1
      so the card can't be 0) */
   
   card = rand() % 10 + 1;
   printf ("It's a %d.
", card);
}
Source by modules.lancaster.ac.uk #
 
PREVIOUS NEXT
Tagged: #random #number
ADD COMMENT
Topic
Name
8+5 =