random(10) is not a standard C/C++ routine.
In C it is rand() which returns a random float value. If I remember correctly it is a number between 0.000000 and 1.000000.
Here is a little example program that I wrote prodices a int value. Which you could use to create a routine like random(10). 0- 10
/* dicerole.c By Eric Stringer v1.0 2000
Gives a random dice role, could be use with Dungeon and Dragons.
Example of the passing arguments to a program.
This is a dos application.
/Sx where x is number of sides, default is six.
/Dx where x is number of dice to be thrown, default is one.
/Tx where x is number of throws to me made, default is one.
/H help message
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
/* Globle varibles */
int sides = 6;
int number = 1;
int throws = 1;
void throwdice();
void help();
void readarg(int, char);
void main(int argc, char *argv)
{
srand( (unsigned)time( NULL ) ); // Seed the random generator
if (argc == 0 ) throwdice();
else readarg(argc, argv);
throwdice();
}
void throwdice()
{
float rndf;
float rnd;
float fsides;
int t;
int d;
t=0;
fsides = sides;
printf("Side:%d Number:%d Throws: %d
",sides,number,throws);
while (t < throws)
{
d=0;
while (d < number)
{
rndf = rand();
rndf = (rndf / RAND_MAX);
rnd = ((fsides - 1) * rndf) + 1;
printf(“Dice: %2.0f ;”,rnd);
d++;
}
printf("
");
t++;
}
}
void readarg(int c, char *v)
{
int x;
char ch[32];
char cx[32];
int y = 0;
int l;
int t;
x = 0;
for (x=1; x<c; x++)
{
strcpy(ch, v);
l = strlen(ch);
for (y=0; y<l;y++)
{
if ( strchr( ch, ‘/’) == NULL ) break;
y++;
if ((ch[y] == ‘S’) | | (ch[y] == ‘s’))
{
y++;
t=0;
while (y<l)
{
cx[t] = ch[y];
t++;
y++;
}
sides = atoi(cx);
if (sides < 2) sides = 6;
break;
}
if ((ch[y] == ‘D’) | | (ch[y] == ‘d’))
{
y++;
t=0;
while (y<l)
{
cx[t] = ch[y];
t++;
y++;
}
number = atoi(cx);
if (number < 1) number = 1;
break;
}
if ((ch[y] == ‘T’) | | (ch[y] == ‘t’))
{
y++;
t=0;
while (y<l)
{
cx[t] = ch[y];
t++;
y++;
}
throws = atoi(cx);
if (throws < 1) throws = 1;
break;
}
if ((ch[y] == ‘H’) | | (ch[y] == ‘h’))
{
help();
break;
}
}
}
}
void help()
{
printf("Dicerole by Eric Stringer V1.0 2000
");
printf("produces random dice rolls.
");
printf("Options:
“);
printf(”/Sx; where x is number of sides.
“);
printf(”/Dx; where x is number of dice to be thrown.
“);
printf(”/Tx; where x is number of times to throw dice.
“);
printf(”
");
}
Originally posted by kentut:
[b]How do I create a random number?
I searched on the web and got this example:
int x = random(10);
I tried it but it didn’t work, do you know what header file I should add or anything?
What would be the outgoing value of that line? random numbers between 0 and 10?
What if I have:
float y = random(0.1);
What would be the range possible numbers?[/b]