question about whole random numbers ?????

Game Editor comments and discussion.

question about whole random numbers ?????

Postby tinkergnome » Tue Nov 01, 2011 7:02 pm

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.
User avatar
tinkergnome
 
Posts: 16
Joined: Wed Oct 12, 2011 5:11 pm
Score: 1 Give a positive score

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

Postby skydereign » Tue Nov 01, 2011 8:57 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby tinkergnome » Wed Nov 02, 2011 10:30 am

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.
User avatar
tinkergnome
 
Posts: 16
Joined: Wed Oct 12, 2011 5:11 pm
Score: 1 Give a positive score

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

Postby tinkergnome » Wed Nov 02, 2011 10:41 am

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 ???
User avatar
tinkergnome
 
Posts: 16
Joined: Wed Oct 12, 2011 5:11 pm
Score: 1 Give a positive score

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

Postby skydereign » Wed Nov 02, 2011 10:49 am

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)).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby Game A Gogo » Wed Nov 02, 2011 11:38 am

what about using (int)rand(256);?
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

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

Postby skydereign » Wed Nov 02, 2011 11:44 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby Game A Gogo » Wed Nov 02, 2011 11:49 am

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?
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

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

Postby tinkergnome » Wed Nov 02, 2011 1:15 pm

Game A Gogo wrote: what about using (int)rand(256);?

it does work nicely thanks :D thank you very much for the input.
User avatar
tinkergnome
 
Posts: 16
Joined: Wed Oct 12, 2011 5:11 pm
Score: 1 Give a positive score

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

Postby skydereign » Wed Nov 02, 2011 8:43 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest