Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 cstdlib.h Random numbers
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
Wolf_Destiny




PostPosted: Mon Mar 05, 2007 5:15 pm   Post subject: 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
 
Sponsor
Sponsor
Sponsor
sponsor
klopyrev




PostPosted: Mon Mar 05, 2007 5:28 pm   Post subject: 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



stdlib.h
 Description:

Download
 Filename:  stdlib.h
 Filesize:  4.76 KB
 Downloaded:  829 Time(s)

 
Wolf_Destiny




PostPosted: Mon Mar 05, 2007 6:16 pm   Post subject: 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.
CPP:

#include <stdlib.h>
#include <time.h>
#define RGB16(r,g,b)  ((r)+(g<<5)+(b<<10))

//... ...

void fade (int amount)
{
time_t thetime = time (NULL);
        srand((unsigned int)thetime);
        int a=0;
        for (a=0 ; a < amount  ;a+=1)
        {
                int tempx1,tempy1,tempx2,tempy2;
                tempx1 = (int)random(240);
                tempy1 = (int)random(160);
                tempx2 = (int)random(240);
                tempy2 = (int)random(160);
                //line_int (tempx1,tempy1,tempx2,tempy2,10,0,0,0);
        }
}
int main()
{
        //clear();
        //line_int (1,1,240,160,10,31,31,31);
        while (1==1)
        {
                fade (100);
        }
}


The error from GCC was:
Quote:
In function 'fade':
undefined reference to 'time'
undefined reference to 'random'
undefined reference to 'random'
undefined reference to 'random'
undefined reference to 'random'
collect2: ld returned 1 exit status
 
Cinjection




PostPosted: Mon Mar 05, 2007 6:22 pm   Post subject: Re: cstdlib.h Random numbers

You also have to make sure that you put these files in the correct directory. Depending on your compiler, it's different just look for an Include folder and copy and paste the file there. There it's a system header file so you can use the angle brackets.
#include <cstdlib>

If that doesn't work, try adding the .h, and if it still can't see it, directly link to the header files using quotes, like this

#incldue "C:\My files\CPP\cstdlib.h"
 
Wolf_Destiny




PostPosted: Mon Mar 05, 2007 6:41 pm   Post subject: Re: cstdlib.h Random numbers

Thanks for answering so quickly! But I think the compiler can find the files (If I change the name it tells me that it can't find the files) but I tried your suggestions and they didn't work.
Heres the output from my command prompt:
Quote:

C:\devkitadv\work\hello>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\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




PostPosted: Mon Mar 05, 2007 6:49 pm   Post subject: 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




PostPosted: Mon Mar 05, 2007 7:00 pm   Post subject: Re: cstdlib.h Random numbers

I did include "time.h" in my program, and I was coding according to the guidelines here:http://www.cplusplus.com/time and here:http://tigcc.ticalc.org/doc/stdlib.html#random

Actually it may be a problem with my compiler because even this doesn't work:
CPP:
/* time example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time (NULL);
  printf ("%ld hours since January 1, 1970", seconds/3600);
 
  return 0;
}


giving the error:
Quote:

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




PostPosted: Mon Mar 05, 2007 7:00 pm   Post subject: 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++)

code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;


int main(){

        time_t theTime = time (NULL);
    srand( (unsigned int) theTime);
        int randomNumber = rand();

        cout<<randomNumber;
        cin.get();

        return 0;

}

 
Sponsor
Sponsor
Sponsor
sponsor
haskell




PostPosted: Tue Mar 06, 2007 6:35 am   Post subject: RE:cstdlib.h Random numbers

Or alternatively,

code:
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
   int randomNumber = rand();

   cout<<randomNumber;

   return 0;
}


Or, coverting Cinjection's example to using STL,

code:
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
     time_t theTime = time (NULL);
    srand( (unsigned int) theTime);
    int randomNumber = rand();

    cout<<randomNumber;
    cin.get();

    return 0;
}
 
OneOffDriveByPoster




PostPosted: Wed Mar 07, 2007 1:32 am   Post subject: Re: cstdlib.h Random numbers

Wolf_Destiny @ Mon Mar 05, 2007 7:00 pm wrote:
"undefined reference to `time'" being exactly the same error as before.


Header files are usually different (and may well be incompatible) between implementations of the C standard library. You should be able to compile your code by adding the function declarations for the library functions you are calling.

As for your link time error, I would suppose that your compiler does link to your C library, but you can try adding -lc to your link command. There is a chance that time() is missing though--if you are targetting a embedded system or otherwise using a "freestanding" implementation of C, you won't always have this. Go look in your kit/SDK for an alternative.
 
haskell




PostPosted: Wed Mar 07, 2007 9:40 am   Post subject: RE:cstdlib.h Random numbers

I just forgot <ctime>.

code:
#include <iostream>
#include <algorithm>
#include <ctime>

using namespace std;

int main() {
     time_t theTime = time (NULL);
    srand( (unsigned int) theTime);
    int randomNumber = rand();

    cout<<randomNumber;
    cin.get();

    return 0;
}
 
ownageprince




PostPosted: Tue Mar 13, 2007 10:36 am   Post subject: Re: cstdlib.h Random numbers

here use these functions

[code]

#include <time.h>
void randomize()
{
time_t t;
srand((unsigned) time(&t));
}
int random(int limit)
{
return rand()%limit;
}
int main()
{
int Number;
randomize();
Number = 1+random(13);
cout<<number<<endl;
return(0);
}
[code\]
 
Wolf_Destiny




PostPosted: Tue Mar 13, 2007 12:43 pm   Post subject: Re: cstdlib.h Random numbers

Thank you all for your help! Sorry for taking so long to reply. But I was actually looking on the internet, and I found an ebook on programming for the GBA (I'll post the link when I've got it) which actually mentioned another gba sdk called HamLib. I tried compiling my program, and all of your suggestions on that and it worked perfectly. I've got a working gba rom of what I was trying to do, but I haven't got access to that computer until Thursday. So I'll post a link to it, and to the book sometime around Thurday.

~Wolf_Destiny
 
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: