Random numbers!

Started by
9 comments, last by Galo 20 years, 3 months ago
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
bla bla bla
Advertisement
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]
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;}

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
You need stdlib.h for rand() to work.

[edited by - furby100 on January 7, 2004 3:49:10 PM]
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.
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



bla bla bla
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
cstdlib
#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]
I am the GAME MASTER ...... well not really but.
...

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.

_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.

This topic is closed to new replies.

Advertisement