PDA

View Full Version : Problems with Random Whole Numbers...


rrocks1241
03-14-2008, 02:03 AM
I have a program that I have to complete where it's basically a guessing game.
Alice picks a random number from 1 to 40,
then the user is asked to pick a number, to try to match the random one.
If the number is too high, it will say "Too high"
if the number is too low, it will say "too low"
if the number is the same as random, then it will let the user know as such.

However, my problem is that sometimes the random number will be a decimal.
Like, I will choose 8 and it will be too low, and if I choose 9 it will say "too high".
I have tried setting the variable "RandomNumber" as (ceiling(randomnumber[minimum=0][maximum=5]integerOnly=true)
I have tried with the integer false.
I have tried with the floor function.
I have tried with the round function.

I have also tried just setting:
(RandomNumber[min=0][max=40])
and then I would do
(RandomNumber set value to (ceiling(RandomNumber)))
and I would try with floor and round as well...

none of these have worked!
I have tested so many times, and I'm just stumped.

thanks so much for your time,

-rrocks

hgs
03-14-2008, 01:10 PM
I don't know why this is happening, and so far I've not tried to replicate it. I'd rewrite the testing code to be "too low" if it is less than r - 0.5 "too high" if it is greater than r + 0.5 and where r is your random generated integer, document that this is to work around a bug.

DickBaldwin
03-14-2008, 09:13 PM
I have a program that I have to complete where it's basically a guessing game.
Alice picks a random number from 1 to 40,
then the user is asked to pick a number, to try to match the random one.
If the number is too high, it will say "Too high"
if the number is too low, it will say "too low"
if the number is the same as random, then it will let the user know as such.

However, my problem is that sometimes the random number will be a decimal.
Like, I will choose 8 and it will be too low, and if I choose 9 it will say "too high".
I have tried setting the variable "RandomNumber" as (ceiling(randomnumber[minimum=0][maximum=5]integerOnly=true)
I have tried with the integer false.
I have tried with the floor function.
I have tried with the round function.

I have also tried just setting:
(RandomNumber[min=0][max=40])
and then I would do
(RandomNumber set value to (ceiling(RandomNumber)))
and I would try with floor and round as well...

none of these have worked!
I have tested so many times, and I'm just stumped.

thanks so much for your time,

-rrocks

See http://www.alice.org/community/showthread.php?t=983

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

DrJim
03-15-2008, 11:09 AM
Also note that there is a bug in the random number generator in Alice and you always need to make sure any test is made on floating point numbers (multiply both numbers by 1.0 before the test).

This bug was documented in the bug list and there are several posting in the forum. It also is very inconsistant - most of the time (>80% in the case of the one class test I'm aware of) the bug does not occur.

rrocks1241
03-25-2008, 03:38 PM
Thanks so much for your help!!
I got the program to work most of the time I tested it :)

again, thanks :D