I'm very new to C ++ and OH, so it's probably something very stupid. The code is in the following
inbox type {CUBE, CONE,}; Typefif Structure {ObjType type; Float x; Float Y; Float jade; Int selected; thing; Fixed object objects [] = {{cube, rand ()% 11-4, rand ()% - 10-5, rand ()% - 65-55, 0}, {CNE, rand ()% 11-4, Rand ()% - 10-5, rand ()% - 65-55, 0},}
I make a call in my main session, at the present time, in my current session Want to However, this is causing the same random number every time the program is wrong. Do not understand?
In the form of extremism, main ()
before the beginning of Global. srand ()
calls, and that comes before calling your , you must first declare a global variable with srand ()
initialization Anything can do, like code> static object objects [] :
constant int x = (srand (time (tap)), 10); Fixed object objects [] = {{cube, rand ()% 11-4, rand ()% - 10-5, rand ()% - 65-55, 0}, {CNE, rand ()% 11-4, Rand ()% - 10-5, rand ()% - 65-55, 0},};
But global variables are generally a bad idea, and depend on things like the initial order (which is only properly specified between the objects started in the same translation unit) It's especially bad.
Instead of just use local variables:
int main () {srand (time (tap)); Object objects [] = {{cube, rand (%) 11-4, rand ()% - 10-5, rand ()% - 65-55, 0}, {CNE, rand ()% 11-4, rand ()% - 10-5, rand ()% - 65-55, 0},}; // nearby objects required as foo (objects [0]); Foo (objects [1]); }
The C ++ standard library now provides better features than srand ()
, rand ()
, and Manually create and generate a generator such as std :: mt19937
by using random_device
instead of time (NULL) to generate special conversion results Header
, and use the delivery object: & lt; Random & gt;
)
#include & lt; Random & gt; Int main () {std :: random_device r; Std :: seed_seq seeds {R (), R (), R (), R (), R (), R (), R (), R ()}; Std :: mt19937 eng (seeds); Std :: uniform_int_distribution & lt; & Gt; District; Object objects [] = {{CUBE, dist (eng, {-4, 6}), dist (eng, {-5, 4}), dist (England, {-55, 9}), {CNE , Dist (Inge, {-4, 6}), Dist (Inge, {-5, 4}), Dist (Inge, {-55, 9}),}}; // nearby objects required as foo (objects [0]); Foo (objects [1]); }
Note how it is read, how easy it is, rather than depending on the desired interval, depending on how %
is accompanied by negative numbers He deals with it.
(I'm not even sure that your basic interval was not a mistake, but I honestly reproduced them with a negative number as normal code, which was not standardized until C ++ 11).
No comments:
Post a Comment