Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for random number function 3

Status
Not open for further replies.

Chemakill

Programmer
Aug 17, 2000
37
0
0
CA
I know there is a random number function, and I knew how to use it, but the school's wonderful, up to date machines do not have help installed (to save hard drive space) so could someone please post a good syntax example for the rand() command? I need to generate 10,000 numbers to be inputted into a file. Also, if anyone could point me to an online syntax resource (I've heard rumours of an online help facility), that would be great and would prevent me from ever having to post a question like this again. Thx.
 
#include<stdlib.h>
int main()
{
int x=rand();
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
It doesn't need to be initialised using srand() but if you don't then you'll get the same random(!) numbers every time you run it.
 
Code:
// This seeds the random number generator with the current time
Code:
srand((
Code:
unsigned
Code:
) time(NULL));
Code:
// nRandom will be a pseudo-random number between 0 and 32767
Code:
int
Code:
nRandom = rand();
You don't have to use srand at all, but if you don't you'll get the same series of &quot;random&quot; numbers every time you run the program. If you do use srand, you don't have to provide the return value of the time function but it's probably the best method of getting as close to random as possible.
 
Even, you all are right, practically it is not a problem. John Fill
1c.bmp


ivfmd@mail.md
 
Hello,
also there is something like this:

randomize();
int a = random(16); // Random a number from 0 to 16.


I can't remember which header the command is in but it is very used. Thank you,

JVFF :)
 
Such randoms you can make by yourselves
int RndBetween(int x,int y)
{
return rand()%(y-x)+x;
} John Fill
1c.bmp


ivfmd@mail.md
 
chpicker:

Maybe you can find it there, but I sure can't. What I'm looking for is the standard help files, the type that VB5, Borland C/C++ 4.52 came with, where you could search a list of all keywords, get syntax, explanation and usually even a code example. That isn't installed here, and I really really need it. I just heard a rumour that you COULD get that online, and that's what I've been looking for. Oh well. Suppose I COULD waste a CD and burn the help files so I'd have a reference here, but I was hoping it was available online.
 
hi.
you can go like:

srand(time(NULL));
int x = rand()%100;
//integers between 0 and 99 :)
 
How come I get these errors when I use srand(time(null)) ??

: error C2065: 'time' : undeclared identifier
: error C2501: 'srand' : missing storage-class or type specifiers
: error C2373: 'srand' : redefinition; different type modifiers
: see declaration of 'srand' Error executing cl.exe. *Suzanne*
 
What you are referring to is MSDN online. This usually comes on a couple of CDs with Visual C++ and you would normally install it along with your IDE environment.

MSDN not only lists all the MFC classes and functions but also gives you an index of ALL the C/C++ keywords and explanations (as well as sample code).

MSDN is viewable online also but I get the feeling you are preferring a copy on disk so you can install on your PC?? Well, you are in luck, I have two old copies that you are welcome to have. One came with VJ++ Pro 1.1 and the latest with Visual C++ 6.0 Standard. Just let me know where to send them - my e-mail is: nick@QED-online.com

Incidentally, there's a lot of C/C++ programming books available for sale on ebay.com. Those big thick books (1000+ pages) that normally retail for around $40-50 are available on ebay.com for around $10 each in excellent condition.
I must've bought over $1,000 (retail) worth of programming books for both PC and Mac for around $150!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top