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.

DarkBASIC Discussion / random numbers?

Author
Message
Tucker S
13
Years of Service
User Offline
Joined: 15th Oct 2010
Location:
Posted: 28th Oct 2010 00:47
In dark basic, can i have a command to issue a random number between two numbers instead of just 0 and a number?
bergice
16
Years of Service
User Offline
Joined: 5th Jun 2007
Location: Oslo,Norway
Posted: 28th Oct 2010 02:50


Guess that would work.

51fa1db0ec7c4af52d93a6f5d0e86bc5


Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th Oct 2010 03:51
The same thing but a more literal version.


There's actually a problem with that code, do you know what it is?
If NumA is greater than NumB then the number inside rnd() is negative, which will return an error.

Hmmm this is quite difficult if NumA and NumB are unknown.
Well I'm off to bed now and quite tired so I'm not sure if this is actually hard or not but it is interesting to solve anyway lol.


Do oranges know what colour they are?
Dia
19
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 28th Oct 2010 03:58 Edited at: 28th Oct 2010 04:00
something like this one?




This is not the Sig you are looking for....
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Oct 2010 04:01
Or:



Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Dia
19
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 28th Oct 2010 14:13 Edited at: 28th Oct 2010 14:16
NumC = NumA+rnd(ABS(NumB-NumA))

will give you the wrong answer if NumA > NumB

i.e. if the values are reversed you get:

NumA = 22
NumB = 6
NumC = 22+rnd(ABS(22-6))

and you get a result 22 -> 38 rather than 6 -> 22

This is not the Sig you are looking for....
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Oct 2010 17:31 Edited at: 28th Oct 2010 17:33
Well if you want a range between 6 and 22, you would want it to add 6, rather than 22. I'm sure there is a better way of doing this but: ABS(A-ABS(A-B)) should give the lower number of the two.

Final equation:
NumC=ABS(A-ABS(A-B))+RND(ABS(A-B))

So NumC=6+RND(ABS(22-6)) yields 6+RND(16) which in turn leaves us at a range of 6->22.

Conversely if it is NumC=6+RND(ABS(6-22)) then NumC=6+RND(ABS(-16)) which is NumC=6+RND(16) which is the same as above.

The only hangup about it is that I have heard that the ABS command is a rather slow one, though I can't confirm this. You probably won't see any kind of impact unless you call it constantly along with displaying lots of 3d objects and such.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 28th Oct 2010 17:37 Edited at: 28th Oct 2010 17:48
@BN2
I don't think that'll work with negative numbers.

Formalized a bit, don't know if it is slow:



usage



Enjoy your day.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th Oct 2010 18:20 Edited at: 28th Oct 2010 18:22
Or without using temp

That might be slower though because there's more arithmetic.

[edit] sorry that was sitting waiting to be posted for ages lol


Do oranges know what colour they are?

Login to post a reply

Server time is: 2024-04-19 09:46:34
Your offset time is: 2024-04-19 09:46:34