Page 1 of 1

question about whole random numbers ?????

PostPosted: Tue Nov 01, 2011 7:02 pm
by tinkergnome
hi ... i was wondering about what scripting code could be used with rand & int to generate a whole
number.... i tried using both opcodes in different placements of an argument but it seems i keep
getting this result of X.XXXXX-eXXX - it's very frustrating.

Re: question about whole random numbers ?????

PostPosted: Tue Nov 01, 2011 8:57 pm
by skydereign
Well, gE's rand returns a random real number between 0 and the number given. But, you can cast it to be an int, or use floor/ceil to convert it to an int (and the list goes on). You could also just set it equal to an int.
Code: Select all
int random = rand(256);
int random2 = floor(rand(256));

That will make random equal to some integer between 0 and 255.

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 10:30 am
by tinkergnome
i remembered you or someone helping me with that similar question a while back ... i switched to a newer PC recently my older PC was having some slight problems. i think my RAM cards were slowly failing & the PC was generally old as all h3LL anyways .. :( but ... thankfully i invested in a newer PC and my work in audio & other creative things live on. my former screenname when i was in here back then was nthl0gik,... or something like that. i think i recently tried to login with that and could not access the forum...... i've also found by experimenting that for example: round(rand(2)) <------ achieved what i wanted too.

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 10:41 am
by tinkergnome
i'm curious ..... i just tried that -------> int random = rand(256);
it kept telling me ")" & ";" need to be there ....
how can i get that to work if within a key press event... to toggle a text actor to generate a number ???

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 10:49 am
by skydereign
Yeah, the account was nthl0gik. I was helping you with save/load stuff. And yeah, round is one of those functions that converts floats and doubles to ints. Usually though I'd suggest floor over round though, as that keeps the indexing starting from 0. Anyway, if you wanted to make a text actor display a random number, you can do this.
text_actor -> Key Down Enter (repeat disabled) -> Script Editor
Code: Select all
int random = rand(256);
textNumber=random;

Of course you can also use text instead of textNumber, that way you can prefix it with something.
Code: Select all
int random = floor(rand(256));
sprintf(text, "random: %d", random);

And in these examples I stored the random value into a variable, as chances are you want to do more than just one thing with it. But, if for some reason you only needed it once, you could just replace the variable name with floor(rand(256)).

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 11:38 am
by Game A Gogo
what about using (int)rand(256);?

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 11:44 am
by skydereign
I don't know, never really liked that method, although I did suggest it among others. Just for some reason I prefer not to cast to convert to an int. I guess it really has to do with the fact that casting wasn't meant for rounding, so the rules always seemed kind of fuzzy. Of course, it is consistent, so technically an okay tool. But if the math libraries are preloaded, why not use floor, ceil, or round? Of course it strikes me that the actual code I used, essentially does the same thing, perhaps worse... oops.

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 11:49 am
by Game A Gogo
Well floor are ceil were always strange to me, I remember trying to learn it ages ago, but didn't get what they did so I never used them.
A way to round with my method is (int)(number+0.5); but then again, using round(); is better in this case.
I'm curious on what could be dangerous or simply just a bad method about using (int) or (float) or any other?

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 1:15 pm
by tinkergnome
Game A Gogo wrote: what about using (int)rand(256);?

it does work nicely thanks :D thank you very much for the input.

Re: question about whole random numbers ?????

PostPosted: Wed Nov 02, 2011 8:43 pm
by skydereign
Well, it isn't really a bad method. It's just, casting a float to an int like that loses data. Of course if you plan for the loss, then not a problem. I don't like cutting off parts of the number, you have more control if you intentionally do it yourself (simply because it proves that you intended for it). It isn't a problem for people that know what they are doing (know how casting converts it). Casting to float though isn't a problem, as it doesn't do that.