gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Std Library not working correctly with visual ham

#28039 - compound - Tue Oct 26, 2004 2:17 pm

ok i used the following function to generate a number between 1 and 5

int myRandVar = rand() % (5 - 1 +1) + 1;

this number is then used as a parameter to decide on what type of object im going to create

bob.CreateShape(myRandVar);

but for some reason it just produces object 4

ive tried to debug this by displaying it as text but cant onvert the integer to a string using

itoa(myRandVar, bbb, 5 );

it says itoa is an unidentified function when this shows up on cplusplus.coms ref library

ive included al these to make sure

#include <cstdlib>
#include <stdlib.h>
#include <math.h>


i really havent a clue im sure it shouldnt be this difficult

thanks alot

#29247 - blinky465 - Mon Nov 15, 2004 12:32 am

I'm fairly new to GBA programming using C/VisualHAM, but have programmed in many different languages over the last ten years or so.
In all of the languages I've used, there has been a need to set the random seed in order to see truly random numbers.

As you probably know, when you ask for a random number, the computer simply returns a number from a predefined list of 'random' numbers.
You will need to set the random seed, which basically tells the computer to pick a new starting point in the list before selecting any more numbers.
Setting the random seed to the current time/date in milliseconds usually emulates a true random number.

I don't know what the syntax is for this is in HAM (and it's a while since I did it in C), but should give you a starting point.
(this should explain why your random number generator always returns 4, as for the undefined function, I don't know until I get reacquainted with C!!)

hope this helps.

#29248 - sajiimori - Mon Nov 15, 2004 12:35 am

itoa() is not part of the standard C library. Use sprintf or Dan's GBA-friendly posprintf.

#29293 - Maxamor - Tue Nov 16, 2004 12:24 am

If you're using Hamlib you don't need to convert it to a string to print it out. Have you tried making a call like this:

If you're debugging to VBA:
Code:
ham_VBAText("%d\n", myRandVar);


If you're using the BG layers:
Code:
ham_DrawText(x, y, "%d", myRandVar);


?

Or is there some other detail I'm missing? I believe this would work for debugging purposes, unless you're not using a tiles BG mode, in which case you'll have to use VBAText.

#29295 - dagamer34 - Tue Nov 16, 2004 3:00 am

When you add a header, youj also have to add the library along with it.
_________________
Little kids and Playstation 2's don't mix. :(