rand (& round) functions do not work as expected

Non-platform specific questions.

rand (& round) functions do not work as expected

Postby nthl0gik » Mon Mar 07, 2011 6:56 pm

hi,.... how's it going,
when i try to generate a random number with the rand function,
all i get is a decimal. i know there must be a way to round it up to a whole number and NOT
a decimal. below is the source i tried to use but i suppose the source code is not correct.
or perhaps i dont have much knowledge in how to code it correctly thru C/C++ to make it
effectively usable with a key down event (which is what i mainly wish it to be activated by).

the ways i tried to make it work:

* num1.textNumber=rand(20);
* num1.text=rand(20) ..........returns error (cannot convert from double to ARY[256]CHAR)
* num1.textNumber=num1.textNumber*rand(20);

so if someone who is more knowledgable could point me in the right direction as to solve this problem
i'd be ever so grateful. i could even appreciate it if someone could show me some links to some compatible
C/C++ info to allow me to understand coding in a C/C++ manner since working it up in a visual type way isn't
cutting the mustard. i wish there would seriously be a place where i could look & understand everything i
need to know to accomplish what i want with this & even other programmatic things in life. it's so frustrating......
Last edited by nthl0gik on Mon Mar 07, 2011 7:12 pm, edited 1 time in total.
User avatar
nthl0gik
 
Posts: 11
Joined: Fri Jan 28, 2011 6:24 pm
Score: 0 Give a positive score

Re: rand function does not work as i expected

Postby skydereign » Mon Mar 07, 2011 7:08 pm

Okay, well rand() doesn't work like the rand in C, just so you know. gE's rand function returns a random decimal number from 0 to the number you give it. So, if you wanted a random int, you could do this.
Code: Select all
textNumber=(int)rand(20);


The (int) before it is called casting, and what it does is turn whatever is to the right into an int, basically. text is a string, so if you wanted to use text, instead of textNumber, you could use this.
Code: Select all
sprintf(text, "%d", (int)rand(20));


-Edit
For C stuff, you can search online and get decent results, but for gE functions you might try this.
http://game-editor.com/docs/script_reference.htm
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: rand (& round) functions do not work as expected

Postby nthl0gik » Mon Mar 07, 2011 7:22 pm

thanks once again skydereign for the assist. i was just missing the "(int)" portion of code,
otherwise i would have it well in place. i had thoughts of what i could do with this snip of code.

ideas possible are listed below:

1. upon game load, a random placement of enemy forces with a RND number corresponding to a coordinate
2. a possible element of a shell game type process
3. random point score value generated for a game that would employ such a feature
User avatar
nthl0gik
 
Posts: 11
Joined: Fri Jan 28, 2011 6:24 pm
Score: 0 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest