hey guys!

i am trying to make an algorithm that generates random numbers within a given range ( this range may differ )

i have tried using rand() but i dont like it because the best seed is the time and it doesnt generate numbers random enough.

thing is i dont know where to start or how to accomplish this.

please help :(

Thanks.

Boost.Random would be a good start. Just because rand() doesn't work for you doesn't mean you should write your own version that's better (you'd likely end up with something worse). There are many existing random number generators out there.

i have tried using rand() but i dont like it because the best seed is the time

That's not really an artifact of the generator. What do you think would be a better seed?

and it doesnt generate numbers random enough

I'd love to hear how you determined this. Not that it's an incorrect statement, but a lot of people think rand() is insufficient through hearsay rather than proper testing.

okay.. i really cant think of a better seed but time is not cool..

I'd love to hear how you determined this. Not that it's an incorrect statement, but a lot of people think rand() is insufficient through hearsay rather than proper testing.

i say this because using time as a seed, rand() generates the same number for like 2-3 seconds then changes .. causing me a problem...

a google search did not provide me with any reliable random number generators but thanks for the boost.random library appreciate it very much and ill see if thats what i needed ;D

thanks again :D

i say this because using time as a seed, rand() generates the same number for like 2-3 seconds then changes .. causing me a problem...

That's user error. You're probably doing something like this:

int get_random()
{
    srand((unsigned)time(0));
    return rand();
}

It may be less obvious than that, of course, but the underlying problem is constantly reseeding the generator rather than doing it once (or very infrequently).

I'll take a moment to suggest this article on using rand().

OOOOOOOOOOOOOOOOOOOOOOOOOOOOO MMMMMMMMMMMMMMMMMM GGGGGGGGGGGGGGGG!

ur a LEGEND!

i dont even know how to thank you enuf for this :o

<33333

one more thing .. i get this compiler error when i do srand(static_cast<int>(time(NULL))); i put it at the top of my code after all the includes and variables

this could give you an idea of where it is

SDL_Rect runStick[4];

//clip dimensions for surface
SDL_Rect levelOnePlatform [50];

//event
SDL_Event event;

srand(static_cast<int>(time(NULL)));

class Platform
{

i get the following errors

1>.\main.cpp(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\main.cpp(38) : error C2365: 'srand' : redefinition; previous definition was 'function'

cant seem to make out what they mean because as a function it should not have a type specifier and i have not defined it before i searched the whole file..

Hey, I have a better idea. Do that right when main() starts:

int main()
{
    srand(static_cast<int>(time(NULL)));

    ...

Executable statements generally don't work too well outside of a function body.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.