# random numbers

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?

It is bit off topic isn’t it?
in C there is rand() only i think.
first include stdlib.h
then srand() with some parameter-preferably time.
and then you use rand()
It will generate an integer between 0 and RAND_MAX(defined in stdlib.h)

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 main(int argc, char *argv[])
{

srand( (unsigned)time( NULL ) ); // Seed the random generator

if (argc == 0 ) throwdice();
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++;

}

}

{
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[x]);
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]

Vlasko is right about it generating an integer between 0 and RAND_MAX. It is not a float.

For an example of how to get a number between 1 and 10, all you need to do is something like so…

iRandomValue = (rand() % 10) + 1;

The % is the modulus, which essentially gives you a remainder, so above it would result in 0-9. To shift that to 1-10 you do the +1. Any other range, you should easily be able to figure out on your own.

Edit: Re-read and noticed you wanted 0-10. For that you just use rand() % 11.

[This message has been edited by Deiussum (edited 07-09-2003).]

random() is a Borland library function.