Random function
Author Message
Random function

Does anyone know of a simple funcition that take a random number from a
die....so like a random number from1-6?

Zach

Sat, 21 Apr 2001 03:00:00 GMT
Random function
On Tue, 3 Nov 1998 10:04:54 -0600, Zach Gorman

Quote:

>Does anyone know of a simple funcition that take a random number from a
>die....so like a random number from1-6?

#include <stdio.h>

int simple_funcition (void)
{
int random_number;

for (;;) {
puts ("Roll your six sided die.");
puts ("What is the result of your roll? ");

scanf ("%d", &random_number);
if (1 <= random_number && random_number <= 6)
break;

puts ("Funny roll.  Try again.");
}

return random_number;

Quote:
}

int main (void)
{
printf ("You rolled a: %d\n", simple_funcition ());
return 0;

Quote:
}

An interesting project would be to change the scanf () call into a
call that activates your flatbed imaging scanner, and apply a shape
recognition algorithm of your choice to read the die roll.  How to do
that would be off-topic for this newsgroup, however.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>

Sat, 21 Apr 2001 03:00:00 GMT
Random function

Quote:

>Does anyone know of a simple funcition that take a random number from a
>die....so like a random number from1-6?

See question 13.16 at http://www.eskimo.com/~scs/C-faq/top.html

--
Craig

Manchester, NH
*plonk* right back at ya!  -- "Country Boy"

Sat, 21 Apr 2001 03:00:00 GMT
Random function
-This is for C-

You need the following in main():
srand((unsigned)time(NULL));

here is an example of a program I wrote of a game of paper, rock, scissors
with the computer where I needed to create random answers for the
machine_choice:

char MachineChoice()
{ int machine=rand()%3+1;    --------------------->This is what generates a
random number between 1-3

the "+1" makes it so it doesn't generate 0-2.
printf("The machine choses ");
switch(machine)
{     case 1:
printf("Rock.");
return 'R';
case 2:
printf("Paper.");
return 'P';
case 3:
printf("Scissors.");
return 'S';
}

Quote:
}

Hope this helps.
Kriss
Quote:

>Does anyone know of a simple funcition that take a random number from a
>die....so like a random number from1-6?

>Zach

Sun, 22 Apr 2001 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages