Decimals?
Author |
Message |
Arrow
|
Posted: Wed Feb 12, 2003 4:55 pm Post subject: Decimals? |
|
|
Noob question here...
how do i make "randint(x,y,z)" generate a number with a certain number of decimals? not just a whole number...
btw x, y, and z are declared: "var x:int" |
|
|
|
|
|
Sponsor Sponsor
|
|
|
azndragon
|
Posted: Wed Feb 12, 2003 5:33 pm Post subject: (No subject) |
|
|
Setting your variables as real type would help |
|
|
|
|
|
Tony
|
Posted: Wed Feb 12, 2003 5:53 pm Post subject: (No subject) |
|
|
rand(x:real)
will assign a random floating point (decimal) number between 0 and 1 to variable x
to get a value you want from it, you add the lower range and multiply by highrange - lowrange.
here's what I mean... say you want a 50 to 100 range.
so you do:
code: |
rand(x)
x:=x*(highrange-lowrange)+lowrange
|
where highrange would be 100 and lowrange would be 50
this code will assign a real value to x from 50 to 100. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Arrow
|
Posted: Wed Feb 12, 2003 10:56 pm Post subject: (No subject) |
|
|
Hmm, thanks for the replys, still dont really understand though...
Heres my code:
Quote: var x:int
var y:int
var a:int
put "Enter the Minimum Value"
get x
put "Enter the Highest Value"
get y
randint(a,x,y)
put "A random number between these two is ", a
How would I go about changing x into a "real" number?
var x:real doesnt work |
|
|
|
|
|
Tony
|
Posted: Thu Feb 13, 2003 9:49 am Post subject: (No subject) |
|
|
thats because randint fuction generates a random integer (whole) value, not real. you have to use rand() to generate a random real number between 0 and 1.
code: |
var low:real
var high:real
var x:real
rand(x)
x:=x*(high-low)+low
put "a random number between ", low," and ",high," is ", x
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Dan
|
Posted: Thu Feb 13, 2003 4:07 pm Post subject: a bit more |
|
|
i just whonted to add on to this for poleop who did not know.
an int or intager is a number with no decliampe places (whole number).
ie. 56
an real number is a number with declmale places.
ie. 56.56789
to decaler an var as an int (a var that will only take whole numbers):
to decaler an var as real (a var that has decalmal numbers):
you are going to decaler your veriables as real if you whont to us the rand() comand to get random numbers with random decmail dlaces. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
Tony
|
Posted: Thu Feb 13, 2003 5:18 pm Post subject: (No subject) |
|
|
hah, thats what I love about C++, you can promote your variables in the program and your integer will turn into real if you do an operation with it containing a real number.
No more type mismatches
*caugh*This is getting off topic |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Dan
|
Posted: Fri Feb 14, 2003 7:28 am Post subject: (No subject) |
|
|
ya that is nice. the ony porblem is when going from float to int it will not rownd just cut off the decmals, but it is still better then turings sytem. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Fri Feb 14, 2003 9:18 am Post subject: (No subject) |
|
|
oh, I'm fine with that... if you want to have it round floating point instead of dropping the decimal, you just add 0.5 before converting. It will be rounded perfectly |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
*GoGeTa*
|
Posted: Mon Oct 18, 2004 5:21 pm Post subject: Hey do you need help |
|
|
tell me exactly what you need help on adn send me your program .... i'll try to help |
|
|
|
|
|
wtd
|
Posted: Mon Oct 18, 2004 6:13 pm Post subject: (No subject) |
|
|
tony wrote: hah, thats what I love about C++, you can promote your variables in the program and your integer will turn into real if you do an operation with it containing a real number.
No more type mismatches
*caugh*This is getting off topic
That behavior in C, C++ and various other languages causes a lot of problems, actually. |
|
|
|
|
|
Cervantes
|
Posted: Tue Oct 19, 2004 9:17 am Post subject: (No subject) |
|
|
is that the 'goto' of C/C++? I wonder what kind of problems you are talking about, though: problems with coding it or problems with that method generally being frowned upon (like goto) |
|
|
|
|
|
wtd
|
Posted: Tue Oct 19, 2004 1:20 pm Post subject: (No subject) |
|
|
Well, it's generally preferable for your compiler to point out the fact that you're using an integer where you intended a floating point number to be used. It allows you to find that semantic (rather than syntactic) error right away, rather than waiting until it becomes a problem. |
|
|
|
|
|
Dan
|
Posted: Wed Oct 20, 2004 10:24 pm Post subject: Re: Hey do you need help |
|
|
*GoGeTa* wrote: tell me exactly what you need help on adn send me your program .... i'll try to help
ummm guys this post was made like a year ago |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
|
|