Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Generating a Random Float Number

Author
Message
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 17th Jul 2012 18:26
Hello, is it possible to generate a Random Float Number? From 1.0 to 3.0 for example? Random(1.0, 3.0) only returns integers...

I then thought perhaps taking a whole number and dividing it by 100 would work, but it doesn't. Is there a differnt command for a Random Float?

Many thanks!
The Zoq2
14
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 17th Jul 2012 19:09
Quote: "I then thought perhaps taking a whole number and dividing it by 100 would work, but it doesn't. Is there a differnt command for a Random Float?"


Are you sure about that, don't forget to store it as a float and not integer...
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 17th Jul 2012 19:17 Edited at: 17th Jul 2012 19:19
Hmmm... Ok, perhaps I am doing something wrong, I tried this;



This only ever returns 1.0 or 2.0 and nothing in between...

EDIT: Ah ha... I see what you mean now, they both have to be floats. I assumed you'd start with an int then divide that by 100, when in fact both need to be declared as floats. Perfect, thanks!
The Zoq2
14
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 17th Jul 2012 19:22
I had no idea the first one had to be a float

Glad you solved it though
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 17th Jul 2012 19:51
I think you could leave the first as an integer, if you use this: Float_RandomNumber = Int_RandomNumber / 100.0

100.0 = float. one of the numbers in the calculation has to be float to return a float, you can just let the 100 be a float by writing it as 100.0

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 17th Jul 2012 23:04
You would be better either dividing by an explicit float ie. 100.0 not just 100 or alternatively multiply your integer by 0.01

I use this a lot and it works the multiplication way at least.


this.mess = abs(sin(times#))
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 17th Jul 2012 23:24
Thanks all, I ended up going with this which seems to work well...



Thanks again!
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 17th Jul 2012 23:25
Yeah, just make sure you have one float in there. Just like with DBP If you ever use all integers and need a float you can just throw in '*1.0'.

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 17th Jul 2012 23:27
What you need is: RandomTime = Random(100, 300) / 100.0

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th Jul 2012 00:46
and bear in mind, multiplying by 0.01 is faster than dividing by 100.

Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 18th Jul 2012 02:40
Quote: "and bear in mind, multiplying by 0.01 is faster than dividing by 100."

Is this from timing tests or is there a reference somewhere?

I've always been curious about the difference between multiply/divide vs binary shift for powers of two;

eg. 128/8 vs 128>>3

My tests seem to suggest multiply/divide is faster though historically it was always the opposite.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 18th Jul 2012 13:36
RandomTime# = Random(100, 300)* 0.01 is the quickest yes (on Windows at least). Not sure about historically and perhaps this might differ depending on the platform?

Either way that's the way I use it more often than not. I only divide by a float if it's not being used every loop and it makes the code easier to understand.


this.mess = abs(sin(times#))
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 18th Jul 2012 22:54
Quote: "Is this from timing tests or is there a reference somewhere?"

I do believe this comes from there being less instructions required to do a multiplication in assembly versus division.
More detailed answers here.

Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 19th Jul 2012 07:47
The raw instruction timings aren't really relevant since AppGameKit BASIC programs are running on VM. So the bulk of the mult/division operation codes execution time really in VM's opcode fetch, rather than the operation.

Login to post a reply

Server time is: 2024-05-05 17:22:15
Your offset time is: 2024-05-05 17:22:15