
-----------------------------------
upthescale
Mon Jun 26, 2006 4:38 pm

Real Variables
-----------------------------------
Ok i am trying to make a bad guy. Now i have my guy and his variables are x and y and they are real.


    drawoval (round (x), round (y), 10, 10, green)


I understand i have to put round.

but when i am setting a value for my bad guys, (they are real not int)

i get an error


    randint (badguy (i).badx, 100, 100)
    randint (badguy (i).bady, 100, 100)

there error states that the argument is the wrong type. Do i have to add round in there? if so where? because everytime i try to add round i get an error!

thanks

-----------------------------------
Clayton
Mon Jun 26, 2006 4:48 pm


-----------------------------------
why dont you use Rand.Int to begin with? now, look at the parameters for randint


randint (variable, lowerbounds, upperbounds : int)


so therefore, you need to have your identifier as the int type. i dont know for sure but i think there is a command to change your real to an int (kinda like intstr, and strint) but i dont know for sure

-----------------------------------
richcash
Mon Jun 26, 2006 4:51 pm


-----------------------------------
You can only use randint with an int type variable. You can make a new variable called r (int) and then randomize that.
randint (r, 0, 100)
badguy (i).badx := r
randint (r, 0, 100)
badguy (i).bady:= r
If you for some reason want to randomize to a real value, then :
rand (badguy (i).badx)
rand (badguy (i).bady)
randint (r, 0, 100)
badguy (i).badx += r
randint (r, 0, 100)
badguy (i).bady += r

-----------------------------------
Clayton
Mon Jun 26, 2006 4:55 pm


-----------------------------------
or how about you do this:
randomize an integer, then add that to your real value. after that use Rand.Real to add a decimal value to it :D

-----------------------------------
upthescale
Mon Jun 26, 2006 4:55 pm


-----------------------------------
well i wanna make an aimer fer my guy, and i want the aimer to rotate nicely so  i will use cos and sin, so i ened ot make the bad guys real, so how can i position them in the screen badx and bady ranomding with round

-----------------------------------
Clayton
Mon Jun 26, 2006 4:57 pm


-----------------------------------
^read above post^

-----------------------------------
[Gandalf]
Mon Jun 26, 2006 8:07 pm


-----------------------------------
I guess that would be an interesting way of going about things, but there are also a few more appropriate methods for doing this.  The simplest being:
You're looking for a decimal value?  Well, you really only care about the first few decimal places if you are using graphics, since it'll be rounded off when you're drawing the pixel anyway.  So why don't you simply divide a large random integer by some specific amount:
var num : real
for i : 1 .. 5
    num := Rand.Int (1, 10000) / 100
    put num
end for

-----------------------------------
Clayton
Mon Jun 26, 2006 8:16 pm


-----------------------------------
hm ive never thought of doing it that way, ive always did it the way i described above :D your way is simpler though, although you may end up with an int in cases where you want a real :(

-----------------------------------
Bored
Thu Jun 29, 2006 6:19 pm


-----------------------------------
fcn RandReal (num1, num2 : real) : real
    result num1 + Rand.Real * (num2 - num1)
end RandReal
Voila, an real number equivalent to Rand.Int :D [/code]

-----------------------------------
Clayton
Thu Jun 29, 2006 6:58 pm


-----------------------------------
actually it should look something like this: 


fcn RandReal (lower_num, upper_num : real) : real
    result Rand.Int ( ceil (lower_num), floor (upper_num)) + Rand.Real
end RandReal


would give you something a bit more along the lines of what you would need :D

-----------------------------------
Bored
Thu Jun 29, 2006 10:58 pm


-----------------------------------
Actually that will not yeild acurate results. Let's say you want a number between 7.6 and 8.9, well your way will choose a random number between 8 and 8, thus choosing 8 and will then add a random real number from 0-1 thus giving you a random number from 7.6 to 8.9. Meanwhile my way will give less decimals the larger the gap. So to I have developed on that merges both ideas. Basically if the difference between the two number is less then one it will use my old method, as it is fairly acurate for that. But if the difference is over one it will use basically the same logic as mine to find a random number between lower and upper - 1. Then it will add the extra digits by finding a random number from 0 to 1. Thus you have lets say min of 5 and max of 8. You have a random number from 5-7 and a random number fromm 0-1 added to give you a random number from 5-8. The code is as follows
fcn RandReal (num1, num2 : real) : real
    if max (num1, num2) - min (num1, num2) >= 1 then
        result min (num1, num2) + Rand.Real * (max (num1, num2) - min (num1, num2) - 1) + Rand.Real
    else
        result num1 + Rand.Real * (num2 - num1)
    end if
end RandReal

-----------------------------------
Bored
Thu Jun 29, 2006 11:07 pm


-----------------------------------
Actually now that i look at the logic, there is one fundamental flaw in the last function i made. Though it may give more digits it is biased. It's bias comes in the fact that all numbers < min + 1 or > max - 1 have less of a chance of being picked the other numbers. This is because there is less posible ways to make that number. It's sorta like in the way rolling two dice does not unbiasdly give you a number from 2-12. The number min can only come if you get min and 0, whereas something in the middle has many more posibilities as you can get a number from num - 1 to num and the then get the correct value on the second Rand.Real. This means that with lower numbers there is a noticable lack of numbers within 1 of the max and min. Now I would recomend this if dealing with large numbers where the difference is insignifigant yet you still need the extra digits (very rare). But for most situations the first function i posted will work best.

-----------------------------------
Clayton
Fri Jun 30, 2006 11:02 am


-----------------------------------
actually i am going to modify this a bit...


fcn RandReal (lower_bound, upper_bound : real) : real
    result (Rand.Int (lower_bound * 1000, upper_bound * 1000) + Rand.Real) / 1000
end RandReal


This way allows for decimals (up to 3 places) and is fairly unbiased, however you will never be able to find the perfect Rand.Real equivalent to Rand.Int...

-----------------------------------
Cervantes
Fri Jun 30, 2006 4:07 pm


-----------------------------------
Let's slow down, for a minute, guys. I think the purpose of this topic was to initialize a real variable with a random integer. 

Instead, we seem to have come up with all sorts of ways to randomly create a real number.

That's missing the mark, from where I sit. All we want to do is initialize the bad guy's position on the screen. Give him an x and y coordinate. We don't care if he's at x = 40 or x = 40.03828439238793294.

So, this entire problem would never have been asked if upthescale used Rand.Int in the first place, and not randint. Go Holtsoft! how many times have I wished you would remove 'randint' entirely.

For example:

var pi : real
pi := Rand.Int (0, 4)
put pi


The reason this works is the reason the following quote is wrong.

lthough you may end up with an int in cases where you want a real

An integer is a real. If R is the set of all real numbers, and I is the set of all integers, I is a subset of R. Anywhere a real number can go, an integer can go. 

Now, we have to understand automatic promotion. If we are expecting a real number to be assigned to our variable, `pi', but we are instead given an integer, that integer is automatically promoted to be a real number. If we are given the integer 4, it is promoted to be the real 4.00000000....
