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.

Newcomers DBPro Corner / random number between -1 and minus 1.1

Author
Message
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 29th May 2011 19:33
Hi all,

I'm new to the forum and I'm quite new to programming.

I need to assign a number between minus 1 and minus 1.1 to a variable to add to the speed of my object when it bounces off a wall but I have no idea how to do it.
I also need the number to be different every time.
I am aware of the rnd() command i.e number=rnd(255) but I have no idea how to specify a range between 2 numbers or how to use it with a real number (float)

I'm not asking for anyone to write code for me just a little point in the right direction would be enough

DarkBasic is great and looking through these boards it seems to have a great, helpful active community behind it which I am very happy to join and hope I can contribute to it one day.

Many thanks,

Paul.

P.s, I will post my code on here when it is finished
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th May 2011 19:39
Welcome

Firstly, you should use

RANDOMIZE TIMER()

to properly randomise your results. This statement uses the Timer as a seed for the random number system.

RND() will generate a number between 0 and the number you specify as the parameter. You need to manipulate this to get your result. You don't specify the number of decimal places, so adjust this example to suit.

generate a number between 0 and 1000:

MyNum# = RND(1000)

Now divide by 10000

MyNum# = MyNum# \ 10000.0

You now have an answer between 0.0000 and 0.1000
Subtract this from -1.0 to get your value.
Now add it to your speed variable.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 29th May 2011 19:43
Hi,

Thanks for the quick response.
I'll try that and let you know how I get on.

Paul
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 29th May 2011 20:22
It works like a charm thanks again but I do have one more question.
What happens if I leave out the RANDOMIZE TIMER()?

Thanks again,

Paul
Mulderman
20
Years of Service
User Offline
Joined: 8th Jan 2004
Location: C:\\
Posted: 29th May 2011 20:50
You will get repeating numbers and not random numbers.
Well computers can't produce 100% true random numbers but RANDOMIZE TIMER() makes them look more random.

If you leave that out, you almost can guess the numbers it generates for you.

I always recommend you to use it.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th May 2011 20:58 Edited at: 29th May 2011 20:59
You can use RANDOMIZE to your advantage. For a given seed you'll always get the same sequence of "random" numbers. I've used it in the past to collate a dozen or so random sequences that work for my scenario.

Without it, you'll always start at seed 0.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 29th May 2011 20:59
how often should I use it?
in my loop I'm using RND() 3 times. I used RANDOMISE TIMER() before the first RND() should I use it before the 2nd and 3rd?

Thanks,

Paul
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 29th May 2011 21:52
No, you would generally use the RANDOMIZE command only to reset the random seed to a known starting point.

In addition, DBPro does the equivalent of an RANDOMIZE TIMER() when you run your program, so that isn't needed at all.

Run the following code several times - you'll get a different value every time:


sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 29th May 2011 22:06
Thanks for the response but now I am confused.
Are you saying that using randomize timer() will make no difference?
if so, when does it make a difference?

Maybe I should explain the context I am RND() in...


I use it once to find a number between -1 and -1.1 so the amount of speed added to my ball object is different every hit like this....




then I use it again to select a sound file to play when the ball is hit by a bat.

I set up a simple commentary effect by using 10 sound files for each player generated by a SAPI 5 voice program.
Every time a player hits the ball one of the sound files is played at random.

here is the RND() code


later on in the code i have

play sound s

it works, not sure if it's the best way...

I use RND() to select the sound files for player 2 also
the code is the same above but with different variable names.

Do I need RANDOMIZE TIMER()?

Thanks in advance
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 30th May 2011 09:53
Quote: "In addition, DBPro does the equivalent of an RANDOMIZE TIMER() when you run your program, so that isn't needed at all."


I didn't know that, when did that sneak in? It hasn't always been that way I'm sure.

Quote: "Thanks for the response but now I am confused.
Are you saying that using randomize timer() will make no difference?
if so, when does it make a difference?"


Random numbers are based on a table (~32,000 numbers). You set the seed and then RND uses the next entry in the sequence each time you call it. Imagine the table is just 10 numbers for a moment:

5 8 1 9 2 3 10 6 4 7

If your seed is 3 (RANDOMIZE 3) then the sequence you would get is

1 9 2 3...

If your seed is 9 (RANDOMIZE 9), it would loop and you would get

4 7 5 8...


So, when you RANDOMIZE TIMER() you simply select a new, unknown starting point, based on the value in TIMER() at the point you call it. After that, you follow the sequence until you RANDOMIZE again.

Given there are ~32,000 random numbers, it's extremely unlikely you'll get a situation where a player remembers what's going to happen next. But if you want to randomize it even further then you can call it again.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 30th May 2011 09:58
Thanks for the detailed response.
I appreciate you all taking your time to explain things

Login to post a reply

Server time is: 2024-11-16 21:58:42
Your offset time is: 2024-11-16 21:58:42