Jump to content
bma

Random float number

Recommended Posts

Hello 

 

I have a very simple question. 

If i want to create a random number between 0 and 1000 but it has to be float, how do it do it?

 

I tried:

float NA = rand()%1000.f; 

 

But it gave me an error. 

Link to comment
Share on other sites

Aint simple

 

Try

float NA = ((float)Math::rand()/(float)Math::RANDMAX)*1000;

Not sure if it works, scripts always act weird.

 

Try adding the following below that to see what value it spits out (in the logfile):

System::Log("NA: %d", (int)NA);

Good luck!

Link to comment
Share on other sites

Do you need to generate a random float? (ie. 123.4552, or 12422.7844)? Or do you just need a random integer in a float data type?

 

If you just need an integer in a float type, a simple Type-Cast as hoppah demonstrated above will work sufficiently. Since you're going from less precision (integer) to more precision (floating point number) the game shouldn't give you any grief. YOu start running into problems when you try and explicitly cast precise number formats into less percise formats such as float to integer. Even then it would usually only throw a Warning which I doubt the game even does.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...