User forums > General (but related to Code::Blocks)

random function question

<< < (2/4) > >>

takeshimiya:

--- Quote from: TDragon on April 04, 2006, 12:42:12 am ---Because it didn't #include <ctime> at the time I made my post.

--- End quote ---
Yup, those things happens. However, it *did* compiled fine in g++.
Somehow, the stdlib++ includes somewhere the function time(), which maked it compile and run fine. :P

TDragon:
My apologies; your original C++ sample does indeed compile under MinGW32/GCC (assuming one adds a wrapping main function).

takeshimiya:

--- Quote from: TDragon on April 04, 2006, 12:50:55 am ---My apologies; your original C++ sample does indeed compile under MinGW32/GCC (assuming one adds a wrapping main function).

--- End quote ---

BTW, I'm very surprised that this compiles fine with no warnings under MinGW32/G++ (compiled with -Wall -W -pedantic):


--- Code: (cpp) ---#include <iostream>

int main()
{
srand(time(0));
for(int i = 0; i < 10; i++)
    printf("Random number #%d: %d\n", i, rand());
}
--- End code ---

stdlib++'s <iostream> is doing nasty things here. :shock:

Notice how srand(), rand(), and printf(), works without having to include <cstdlib>.
And <iostream> internally must be including <stdlib.h> instead of <cstdlib>, because notice how putting std:: qualifier is not requiered.

Also notice how including <ctime> was not requiered, neither qualifying time() with std::.

Deamon:

--- Quote from: TDragon on April 04, 2006, 12:21:27 am ---#include <stdlib.h> // if using C++, #include <cstdlib>; may not be necessary in C
#include <time.h> // if using C++, #include <ctime>

// Seed the RNG
srand(time(0));

int RandomIntInRange(int low, int high)
{
   return (int)(rand() / (RAND_MAX / (double)(high - low))) + low;
}

--- End quote ---

Are you sire about it ?

When i compile it it tells me:

main.cpp:20: ISO C++ forbids declaration of `srand' with no type
main.cpp:20: `int srand' redeclared as different kind of symbol
D:/Programme/CodeBlocks/share/CodeBlocks/plugins/compilers/MinGW/include/stdlib.h:362: previous
   declaration of `void srand(unsigned int)'
Process terminated with status 1 (0 minutes, 3 seconds)

When i put it inside RandomIntInRange() or main then it works but why doesn't it work globaly ?

And TDragon, is see you made your own function for random number with a range resolution! Does that mean there is now specialized function for it in the ansi libs ?


--- Quote ---There's probably an unnecessary cast in there, but don't disparage my parentheses -- I use them to indicate my thought processes when I come back to the code a few years down the road.

--- End quote ---

What ?

regards,
Deamon

TDragon:

--- Quote from: Deamon on April 04, 2006, 01:54:41 am ---When i compile it it tells me:

main.cpp:20: ISO C++ forbids declaration of `srand' with no type
main.cpp:20: `int srand' redeclared as different kind of symbol
D:/Programme/CodeBlocks/share/CodeBlocks/plugins/compilers/MinGW/include/stdlib.h:362: previous
   declaration of `void srand(unsigned int)'
Process terminated with status 1 (0 minutes, 3 seconds)

When i put it inside RandomIntInRange() or main then it works but why doesn't it work globaly ?

--- End quote ---
srand(time(0)); is a function call. Function calls must be inside other functions or used as expressions -- they don't qualify as statements in the global scope. You should place it in your main function or initialization function (if you have one).


--- Quote ---And TDragon, is see you made your own function for random number with a range resolution! Does that mean there is now specialized function for it in the ansi libs ?

--- End quote ---
That's correct, there is no function in ANSI C that returns a random number within a custom range.


--- Quote ---
--- Quote ---There's probably an unnecessary cast in there, but don't disparage my parentheses -- I use them to indicate my thought processes when I come back to the code a few years down the road.

--- End quote ---
What ?

--- End quote ---
Nothing. :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version