
-----------------------------------
Wolf_Destiny
Mon Mar 05, 2007 5:15 pm

cstdlib.h Random numbers
-----------------------------------
Pretty Quick question:

I've been trying to port one of my turing programs into C++ in order to try and learn the language. However I've also been using the devkitadv GBA compiler. That means that I haven't got access to alot of libraries unless I can both find the file, and be lucky enough that it compiles. I've already had to make my own recursive line command (which is probably slower than Bresenham's algorithm, but was only 6 lines) and now my problem is the need of a random number generator. The tutorial here:http://www.cprogramming.com/tutorial/random.html suggested "cstdlib" which is simple enough, but my compiler can't find it, so I probably haven't got it. So my question is: Where can I find it, or does someone have it to share?

Thanks
~Wolf_Destiny

-----------------------------------
klopyrev
Mon Mar 05, 2007 5:28 pm

Re: cstdlib.h Random numbers
-----------------------------------
Hmm... I think you have made a mistake. stdlib.h is a C standard library. cstdlib is a C++ standard library. cstdlib.h is a hybrid. Don't think a file like that exists. Here is one of them. If you need the other, I could send it to you.

KL

-----------------------------------
Wolf_Destiny
Mon Mar 05, 2007 6:16 pm

Re: cstdlib.h Random numbers
-----------------------------------
0.0 wow that was really simple, thanks! Okay so I've got it all coded, but it keeps giving me an error when I try to compile.

#include 
#include 
#define RGB16(r,g,b)  ((r)+(gset path=C:\devkitadv\bin;C:\WINDOWS\system32;C:\WINDOWS
;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:
\devkitadv\bin;C:\javac\bin;

C:\devkitadv\work\hello>gcc -o hello.elf hello.c -lm
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccZy3PBg.o: In function `fade':
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccZy3PBg.o(.text+0x280): undefined ref
erence to `time'
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccZy3PBg.o(.text+0x2b8): undefined ref
erence to `random'
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccZy3PBg.o(.text+0x2c8): undefined ref
erence to `random'
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccZy3PBg.o(.text+0x2d8): undefined ref
erence to `random'
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccZy3PBg.o(.text+0x2e8): undefined ref
erence to `random'
collect2: ld returned 1 exit status

C:\devkitadv\work\hello>objcopy -O binary hello.elf hello.gba
objcopy: hello.elf: No such file or directory

C:\devkitadv\work\hello>pause
Press any key to continue . . .

-----------------------------------
klopyrev
Mon Mar 05, 2007 6:49 pm

Re: cstdlib.h Random numbers
-----------------------------------
You are using functions that are not actually in the stdlib include file. Here is what the include file says about random

long random(void);
long srandom(int _seed);

There is no time function in stdlib either. You have to include time.h or ctime.

KL

-----------------------------------
Wolf_Destiny
Mon Mar 05, 2007 7:00 pm

Re: cstdlib.h Random numbers
-----------------------------------
I did include "time.h" in my program, and I was coding according to the guidelines here:/* time example */
#include 
#include 

int main ()
{
  time_t seconds;

  seconds = time (NULL);
  printf ("%ld hours since January 1, 1970", seconds/3600);
  
  return 0;
}


giving the error:

C:\devkitadv\work\test>set path=C:\devkitadv\bin;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\
devkitadv\bin;C:\javac\bin;

C:\devkitadv\work\test>gcc -o new.elf new.c -lm
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccjJY1nj.o: In function `main':
/cygdrive/c/DOCUME~1/steven/LOCALS~1/Temp/ccjJY1nj.o(.text+0x18): undefined refe
rence to `time'
collect2: ld returned 1 exit status

C:\devkitadv\work\test>objcopy -O binary new.elf new.gba
objcopy: new.elf: No such file or directory

C:\devkitadv\work\test>pause
Press any key to continue . . .

"undefined reference to `time'" being exactly the same error as before.

-----------------------------------
Cinjection
Mon Mar 05, 2007 7:00 pm

Re: cstdlib.h Random numbers
-----------------------------------
EDIT: You did post the code. Thanks.

The code you posted, first of all is C, not C++. Try to compile this. It works on my compiler (MSVC++)


#include 
#include 
#include 

using std::cout;
using std::cin;


int main(){

	time_t theTime = time (NULL);
    srand( (unsigned int) theTime); 
	int randomNumber = rand();

	cout