Computer Science Canada

Decimals?

Author:  Arrow [ 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"

Author:  azndragon [ Wed Feb 12, 2003 5:33 pm ]
Post subject: 

Setting your variables as real type would help Very Happy

Author:  Tony [ Wed Feb 12, 2003 5:53 pm ]
Post 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.

Author:  Arrow [ Wed Feb 12, 2003 10:56 pm ]
Post 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

Author:  Tony [ Thu Feb 13, 2003 9:49 am ]
Post 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

Author:  Dan [ 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):

code:

var nameofvar:int


to decaler an var as real (a var that has decalmal numbers):

code:

var nameofvar:real


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.

Author:  Tony [ Thu Feb 13, 2003 5:18 pm ]
Post 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 Very Happy

*caugh*This is getting off topic Confused

Author:  Dan [ Fri Feb 14, 2003 7:28 am ]
Post 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. Smile

Author:  Tony [ Fri Feb 14, 2003 9:18 am ]
Post 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 Very Happy

Author:  *GoGeTa* [ 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

Author:  wtd [ Mon Oct 18, 2004 6:13 pm ]
Post 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 Very Happy

*caugh*This is getting off topic Confused


That behavior in C, C++ and various other languages causes a lot of problems, actually.

Author:  Cervantes [ Tue Oct 19, 2004 9:17 am ]
Post 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)

Author:  wtd [ Tue Oct 19, 2004 1:20 pm ]
Post 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.

Author:  Dan [ 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


: