PDA

View Full Version : Confused about random numbers



sandyrepope
12-02-2007, 10:33
I've just started trying to learn how to use thinbasic and am having a problem. I've tried everything I could think of using both rnd and randf to get random numbers between 3 and 18 inclusive. In other words the numbers would always be greater than 2 and also less than 19.

So far, nothing works for me. Could someone clue me in on how to handle this?

Petr Schreiber
12-02-2007, 11:00
Hi,

did you tried this?:


RANDOMIZE TIMER ' Put this on start of script, to create random seed for ... random number generator :)

DIM MyRandom AS NUMBER

MyRandom = RND(3, 18) ' For integer class numbers
msgbox 0, "Integer like: "+FORMAT$(MyRandom)
MyRandom = RNDF(3, 18) ' For floating point numbers
msgbox 0, "Floatie like: "+FORMAT$(MyRandom, "0.00")



Hope you will like thinBasic more as you will explore it :),
Petr

Michael Hartlef
12-02-2007, 11:05
Try this:


Uses "Console"
dim r as double
'Set Console to WINDOW mode (In case it started FULLSCREEN)
Console_NormalScreen
r = rndf(1,3,4)
Console_WriteLine("r="+str$(r,5))

Console_WaitKey

Petr Schreiber
12-02-2007, 12:01
Mike,

nice sample,
I almost forgot about the possible decimal precision specified in RNDF.


Bye,
Petr

ErosOlmi
12-02-2007, 13:22
Hi Sandy (hope it is the name otherwise sorry),

RND has different options. I've checked thinBasic help and I have to say it is not that clear :-[ I will implement for next release.

Anyhow, first thing to do is to call RANDOMIZE (http://www.thinbasic.com/public/products/thinBasic/help/html/randomize.htm) function in order to seed random number generator.
After you can choose from:
RND returns an extended number between 0 and 1
RND(a, b) returns a Long-integer in the range of a to b inclusive
RNDF(a, b) returns an extended in the range of a to b inclusive
RND(NumericExpression) ... more details in next help file.

Here it is a new script version taken from Mike example printing in console line randomly generated from 3 to 18 (inclusive)

Uses "Console"

randomize

dim r as double

Console_NormalScreen
console_writeline("Press any key to contonue")

'---While no input
while len(console_inkeyb) = 0
r = rnd(3,18)
console_printat(format$(r, "000") & " " & timer, 10, r, rnd(1,32))
wend

console_writeline("All done")
Console_WaitKey

Try to change limits and see if numbers are generated correctly.

I will implement help with more info.

Regards
Eros

Michael Hartlef
12-02-2007, 16:31
Mike,

nice sample,
I almost forgot about the possible decimal precision specified in RNDF.


Bye,
Petr


Thanks Petr,
We were posting at the same time, I hope you didn't mind that I still posted my answer.

sandyrepope
12-02-2007, 18:25
Here is the code I've been trying to get to work:



Uses "CONSOLE"

%Menu_Color_Normal = 7

dim Strength as single
dim Intelligence as single
dim Wisdom as single
dim Constitution as single
dim Dexterity as single
dim Charisma as single
dim Accept as string
dim KeyPress as string




RANDOMIZE TIMER ' Put this on start of script, to create random seed for ... random number generator



call Create_Character


function Create_Character()
console_cls
Accept = %false
console_printat("DUNDEON ADVENTURE CHARACTER CREATOR" , 29, 3, %Menu_Color_Normal)
console_printat("<ENTER> TO USE THIS, ANY OTHER KEY TO TRY AGAIN" , 29, 3, %Menu_Color_Normal)
console_printat("STR INT WIS CON DEX CHA" , 29, 7, %Menu_Color_Normal)

while Accept = %false


Strength = rnd(3,18)
Intelligence = rnd(3,18)
Wisdom = rnd(3,18)
Constitution = rnd(3,18)
Dexterity = rnd(3,18)
Charisma = rnd(3,18)

console_printat(Strength , 30, 10, %Menu_Color_Normal)
console_printat(Intelligence , 34, 11, %Menu_Color_Normal)
console_printat(Wisdom , 38, 12, %Menu_Color_Normal)
console_printat(Constitution , 42, 13, %Menu_Color_Normal)
console_printat(Dexterity , 46, 14, %Menu_Color_Normal)
console_printat(Charisma , 50, 15, %Menu_Color_Normal)

KeyPress = console_waitkey(0)

if KeyPress = "[RETURN]" then
Accept = %true
end if

wend











end function



The problem is that after pressing the spacebar a couple of times or more I get numbers outside the range 3-18. I start getting numbers like 70,45,etc.

Still can't figure it out.
Thanks

ErosOlmi
12-02-2007, 18:44
Hi,

now I see the problem. It is just a video clean problem.
When you write a 2 digit number and then write at the same position 1 digit number, if you do not clean the screen it seems number is not correct while you are just printing one char while the second char comes from the previous print.

See here you code with modified print out using FORMAT$.

Let me know.
Eros



Uses "CONSOLE"

%Menu_Color_Normal = 7

dim Strength as single
dim Intelligence as single
dim Wisdom as single
dim Constitution as single
dim Dexterity as single
dim Charisma as single
dim Accept as string
dim KeyPress as string

RANDOMIZE TIMER ' Put this on start of script, to create random seed for ... random number generator

call Create_Character

function Create_Character()
console_cls
Accept = %false
console_printat("DUNDEON ADVENTURE CHARACTER CREATOR" , 29, 3, %Menu_Color_Normal)
console_printat("<ENTER> TO USE THIS, ANY OTHER KEY TO TRY AGAIN" , 29, 3, %Menu_Color_Normal)
console_printat("STR INT WIS CON DEX CHA" , 29, 7, %Menu_Color_Normal)

while Accept = %false

Strength = rnd(3,18)
Intelligence = rnd(3,18)
Wisdom = rnd(3,18)
Constitution = rnd(3,18)
Dexterity = rnd(3,18)
Charisma = rnd(3,18)

console_printat(format$(Strength , "00") , 30, 10, %Menu_Color_Normal)
console_printat(format$(Intelligence, "00") , 34, 11, %Menu_Color_Normal)
console_printat(format$(Wisdom , "00") , 38, 12, %Menu_Color_Normal)
console_printat(format$(Constitution, "00") , 42, 13, %Menu_Color_Normal)
console_printat(format$(Dexterity , "00") , 46, 14, %Menu_Color_Normal)
console_printat(format$(Charisma , "00") , 50, 15, %Menu_Color_Normal)

KeyPress = console_waitkey(0)

if KeyPress = "[RETURN]" then
Accept = %true
end if

wend

end function

sandyrepope
13-02-2007, 01:17
Thanks, Eros, that fixed my problem. It looks like it will take me a while to learn thinbasic.

Do you know of any tutorials around that would help me to learn?
I really do need to learn the basics.

The help file is a little difficult for someone new to learn from.

ErosOlmi
13-02-2007, 01:41
Sorry but we do have any tutorial at the moment. Just examples included in installation (see Samplescripts under thinBasic installation directory).
There are also many examples posted in forum by other users but they are specific to some problem faced in the past.

Anyhow, I will be very happy to help you here in the forum. Maybe not immediate reply but when I have some spare time I'm here. Mastering a new language is not easy at first even if syntax is simple and coherent.
Do not hesitate to ask whatever question or ask for new features. We are very happy to add new keywords or implement current one if we think they can give advantaged to all users.

Ciao
Eros

kryton9
13-02-2007, 04:21
I will be glad to make a video tutorial for you. Let me know if you are interested in console or windows type basic intro.
The videos will be in wmv format if that is ok. You will love thinBasic and we will be glad to help you. I am just a step ahead of you in learning thinBasic, but with the wonderful help here, you make progress quickly.

Petr Schreiber
13-02-2007, 12:26
;)

I am sure krytons tutorial would be very good,
he has great experience in doing such a videos,
I have still some thinEdge ones stored in prominent folder on HDD :)

Forum brainstorm is also good !


Bye,
Petr

kryton9
13-02-2007, 15:06
Petr, your video tutorials are very very good too, don't forget to mention that. In fact you came up with 2 very clever ways of doing them, the gif animation and then the titled and noted videos.

I do mine the easiest way, microphone and capture:)
Which reminds me, when and if you can find the time:
Video with Voice tutorials on the following:
thinEdge:
Terrain Tips
Advanced Modeling
Texturing and UV mapping
Multi-Object Models and Rigging
Animation and use in thinBasic

:)