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 numbers in a range

Author
Message
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 5th Feb 2003 20:30
Thanks in advance for the help. Recently, I fell in love with the DARKbasic environment and i've brushed off the dust from my old Qbasic source code and i've been trying to clear the cobwebs from my mind about the basic language.

I'm working on a project and i've completely forgotten how to use the INT command to generate a random number in a given range. For example, say I wanted to generate a random number value between 115 and 8,387.

The problem with the RND command is that it always starts with 1 as a base number, and for my application, I need to use higher base numbers and specific ranges for my random numbers. Here's what I can remember:

LET X=INT(RND

and then... I draw a blank. Can anyone help me with this most basic of commands?
Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 5th Feb 2003 20:38
You cannot set a range using the RND command itself, but it's really not hard - how about this:

x = 115 + rnd(8387-115)

RND starts from a base of zero, not 1, so this will always give you a value between 115 and 8387 fully inclusive of those values.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Feb 2003 20:39
RND produces integers starting at 0 (the manual/help is partially incorrect).

To get the range you want use this: X=RND(8387-115)+115
or : X=RND(8272)+115
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Feb 2003 20:39
Boo. You beat me to it - but I included the 'integer' bit
Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 5th Feb 2003 20:44
I did But I also fail to see what's wrong in the help file for it? It says:

This command will return a random number between 0 and the range provided. The integer range will be the highest number you want
returned. The return value will be an integer number.

Which just about covers all bases IMHO!

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Feb 2003 20:47
But the syntax shows:

Return Float=RND(Range Value)

Most of the time, you know what you want, and just look for the syntax ... at least I do.
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 5th Feb 2003 20:52
Thanks guys. The solution you've provided should work fine for my app. I know there's a way to do it with the INT command and the RND command but for the life of me I can't remember the syntax. I'm having to reach back to my qbasic 4.5 knowledge to figure it out.

Your solutions are elegant and i'll use them without worrying about what I was trying to recall. Maybe i'll come across it again in one of my qbasic programming manuals.

Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 5th Feb 2003 21:08
What did you want to do with the INT command?! RND returns an integer anyway (heh, thanks Ian) so it's already an "INT".

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 5th Feb 2003 21:22
I know that. I guess the older versions of basic must have required it back in the day although i'm not quite sure why. If I ever did have a good answer to that question i've long forgotten it.

I did learn to generate random numbers within a range using the INT command though. The syntax was similar to this:

let x = int(rnd(100-10)+10)

This would generate an interger between 10 and 100. Once again, I have no idea why I learned it this way but I did. It could have even been a throwback to when I used to do simple door programs for BBS software on my trusty old Apple ][/e. Those were the interger basic years... sigh.

Slowly.. the code returns to my mind. If I wasn't so lazy i'd go dig through my dry storage boxes for my qbasic reference books, but i'm a lazy son of a B....

Thanks guys!

Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 5th Feb 2003 21:25
hmm.. maybe it was so that you could assign an interger value to a string variable, such as x$=int(rnd(100-10)+10)

Maybe that was it...

My mind says, "Reply Hazy, ask again"

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Feb 2003 21:29
No, that's not it. In other versions of Basic the RND() function does not take arguments and returns a floating point value ranging from 0 (inclusive) and 1 (exclusive).

So to get the integer range you want you have to do this:

X=INT( RND() * Range )+Start
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 5th Feb 2003 21:32
That's it. You nailed it IanM.

You win the prize. Unfortunately, it's just a hearty handshake and a pat on the back...

Yes.... no... wait!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Feb 2003 21:38
Cool. I'm in Coventry/UK. When are you delivering?

Login to post a reply

Server time is: 2024-11-23 23:55:53
Your offset time is: 2024-11-23 23:55:53