• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Random numbers!

## Recommended Posts

Galo    118
Hi, im trying to learn C for a while now and im not getting more motivated by this, it''s so damn huge that language, it''s confusing me. anyway, how do i make a random number in C... not c++ for as far as i know its int X = rand(1..9); but that''s not working and i can''t find it anywhere. Galo

##### Share on other sites
Bjorta    122
I think its
int x = rand() % y;
where y is how many different numbers you want.

Oh, and if you want a number between 1 and 10 its rand() % 10+1, because it starts counting from zero.

[edited by - Bjorta on January 7, 2004 1:09:07 PM]

##### Share on other sites
Agony    3452
The suggestion

int x = rand() % y;

will work well enough, but it''s distribution isn''t usually even, if that''s a concern. To get a more evenly distributed random number, here''s two simple functions (one for ints, and one for doubles):

int Random(int Min, int Max){  return (int)((Max - Min + 1) * (double)rand()               / ((double)RAND_MAX + 1.0) + Min);}double Random(double Min, double Max){  return (Max - Min) * (double)rand() / (double)RAND_MAX + Min;}

##### Share on other sites
furby100    102
You need stdlib.h for rand() to work.

[edited by - furby100 on January 7, 2004 3:49:10 PM]

##### Share on other sites
Guest Anonymous Poster
and don''t forget to seed it at the start of the program (call srand and send it the current time), or else you''ll get the same sequence of numbers every time.

##### Share on other sites
Galo    118
quote:
Original post by Anonymous Poster
and don''t forget to seed it at the start of the program (call srand and send it the current time), or else you''ll get the same sequence of numbers every time.

Whats Seeding ?

anaaahhh "You need stdlib.h for rand() to work." you mean stdio.h ?

galo

##### Share on other sites
Guest Anonymous Poster
Nah he means stdlib.h

Since there is no such thing as complete randomness the computer must have a starting psuedo-random value... this is called the seed

kappa    138
cstdlib

##### Share on other sites
Thamior    122
#include"iostream.h"
#include"stdlib.h"
#include"time.h"

int main()
{
//declare variables
int die_sides, rollnum;
//set seed
srand(time(0));
//ask for user input (don't have to its optional)
cout<<"Enter a number sided die";
cin>>die_sides;

//use random function
rollnum = rand()%die_sides;
cout< getch();
return 0;
}
you can replace die_sides with any number and you may do math to add minimums and maximums like so:
rollnum = rand()%monster_dmg+55;

[edited by - Thamior on January 8, 2004 1:39:58 PM]

##### Share on other sites
Drevay    100
...

C++ is a better C, you know - much better.

You''d be surprised at how much more you can do with C++ (or atleast more efficiently, in a faster way).

Learn C++, there is no reason for you to learn C - you are not gaining anything but losing.

Peace.

##### Share on other sites
Nested Block    122
I use a random header file that I just call when I need random numbers. I put it together using Rob McGregor's book Practical C++.

#ifndef __random_h__#define __random_h__#include <cstdlib>#include <ctime>class CRand{public:	CRand() { srand((unsigned)time(0));}	CRand(unsigned seed) { srand(seed); }	int Generate(int min, int max)	{		if (max < 0)max = 0;		if ((min == max) && (min == 0))			return 0;		return rand() % (max + 1 - min) + min;	}};#endif

When you need a random number of any kind (don't forget to call the CRand rand; class in main) just call rand.Generate(minimum number, maximum number) and it does it for you.