Board Logo
« Random number generator »

Welcome Guest. Please Login or Register.
Jan 16th, 2018, 3:42pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Random number generator  (Read 212 times)
SilverSurfer
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Random number generator
« Thread started on: Nov 20th, 2017, 09:19am »

Hello all,

Now retired after 40 years in the I.T. industry and without ever having been involved with programming, Iíve decided to try and learn how to do it. I chose Just Basic because it seemed perfect for someone like me. Iíve had some success with very simple programs Iíve done, however, Iíve now encountered a problem I cannot resolve. Your help please.

It concerns the Random Number Generator. I have two questions about its use. Firstly, can I get it to create more than 1 number each time I run it, Iíd like it to generate 6 random numbers between 1 and 59. Secondly, how do I stop it from creating numbers with up to 8 decimal places?
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Random number generator
« Reply #1 on: Nov 20th, 2017, 11:28am »

Code:
function rand(lo, hi)
    rand = int((hi - lo + 1) * rnd(0)) + lo
end function 


This function will return an integer between lo and hi inclusive.

So 6 between 1 and 59 is (with the above function listed below the main code):

Code:
for i = 1 to 6
   print rand(1, 59)
next 


Of course it is no problem to modify this for fixed decimals as well.

Welcome to the forum!
« Last Edit: Nov 20th, 2017, 11:33am by bplus » User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: Random number generator
« Reply #2 on: Nov 20th, 2017, 2:45pm »

Quote:
Secondly, how do I stop it from creating numbers with up to 8 decimal places?


Function to round to any number of decimal places
Code:
for n = -5 to 5
print "Rounds to ";(n);" decimal places"
for i = 1 to 4
    rand=rnd(0)*1e6
    x = round(rand, n)
    print rand, x, using ( "#########.######", x)  'without USING you'll have only 7 digits printed
next
next

function round(x, n)    'rounds x to n-th digith (involves 1/10^n, obviously
    round=int(x*10^n+.5)/10^n  '+0.5 for rounding to nearest
end function

 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
SilverSurfer
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: Random number generator
« Reply #3 on: Nov 21st, 2017, 01:34am »

bplus and tsh73 thank you both for your prompt replies, I shall follow your advice and see how I get on.
User IP Logged

Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Re: Random number generator
« Reply #4 on: Nov 21st, 2017, 06:39am »

The Building Blocks resource is worth a browse and worth knowing about.

http://justbasic.wikispaces.com/BB___RND

So too the LBPE

http://lbpe.wikispaces.com/rndrandomize
« Last Edit: Nov 21st, 2017, 06:40am by Rod » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls