#Manx help
8 messages in this thread
Hello Mike,
I have another question for you about using this wonderful (and
confusing to me), Manx 3.6a compiler.
I want to use the random generator but don't seem to be able to get it
to work. I see the Synopsis shows: double ran() .It also says that the
value returned will be a number between 0.0 and 1.0 .
How do I get a variable to equal the returned number? Can I put an
argument (somehow) between the brackets? And how do I get the function to
give me a random number between 1 to 50 for example?
My gosh I have another question <smiling>. I noticed quite a few
examples in the vt100 directory. If it's not too much trouble, could you
tell me which ones are actually needed to make a very simple communication
program, as well as any other files I will need?
Thank you for any help you can give me.
Henry PS: Fortunately, I don't have to feed myself by programming
computers.
Henry,
double RandomNumber;
int OneTwoFifty;
RandomNumber = rand();
OneTwoFifty = (INT) (RandomNumber * 50.0 + 0.5);
Should work, assuming that I'm awake today <grin>.
Peter_S (LeCroy Corp)
"Software is never having to say you're finished"
Hi Peter,
Thanks for the help. I'll try this and see if you were awake 🙂
Regards, Henry
You need to use the mod function (%) not multiply (*) to get the range
right.
-Mike
Oops, I typo'ed the function name. He said ran(), double function returning
a number between 0 and 1, not rand() which returns an integer (range
depends on your implementation).
Peter_S (LeCroy Corp)
"Software is never having to say you're finished"
ran() should only be used if you need a floating point random number. If
you're interested in integer stuff (which is the usual case), you say
something like:
foo () {
int val;
val = rand(); }
rand() is similar to ran(), except that it returns an integer between 0 and
32767. If you need a range between say 1 and 50, you'd say something along
the lines of:
val = (rand() % 50) + 1;
-Mike
Great idea… i have been wasting time using a #define Rndm(x) (x *
rand())/MAX_RAND … I like mod better already
Hi Mike,
Thanks thats just what I needed. I tried the other example and it
didn't quite make it.
Henry