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 / Solving Math Problems

Author
Message
Silence
11
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 14th Oct 2012 20:29
I am very new to DBPro and need assistance on a code.

I have fairly no idea how to make DBPro generate 9 random numbers and add the first 8 divide by the 9th and raise it to a power of another randomly generated number.

If I press the number 1 it should generate 9 one digit numbers.
If I press 2 it should generate 9 two digit numbers and continue doing this until the key "5" is pressed where it will generate 9 five digit numbers. Can anyone help me with this?
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Oct 2012 13:10
I'm on a mac right now and it's been a while since I last coded in DBPro but I believe the command you're looking for is rnd().

rnd(n) will return a random number (I think integer) between 0 and n inclusive.

To raise a number to a power use the ^ symbol. a^b raises a to the power of b.

Hope this helps.

Silence
11
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 15th Oct 2012 16:04
And how to I make those randomly generated numbers add themselves together and divide by the ninth number?
Mr909
11
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 15th Oct 2012 23:57
Okay, random number generation is, as he explained, RND(N).

So, if counting 0's in your random number generation, RND(N) would return 0123456789.

If you don't want 0, then you would set your variable for RND(8)+1, obviously. This is oftentimes important in other types of code.

So, to add, we'd first use a variable. Being straightforward, I use things like "num", always going for something practical that's easy to remember.

[SPOILER]
num=RND(9)
[/SPOILER]

That will return a single number. How do you figure out what that number is? Use "PRINT"

[SPOILER]
num=RND(9)

PRINT STR$(num)

WAIT KEY
END
[/SPOILER]

I had to use STR$() in order to "cast" the number variable as a string instead, in order to print it. Also notice I used "WAIT KEY" so you can actually see the program's output before it ends.

Then it's a matter of creating a loop, to get nine numbers instead of one. I always use FOR-NEXT loops, they're easy to understand. Basically, FOR-NEXT runs a variable from one point to another, increasing it each time. So, for nine different numbers:

[SPOILER]
FOR a=1 to 9
num=RND(9)
PRINT STR$(num)
NEXT a
WAIT KEY
END
[/SPOILER]

Writes a variable, displays, then repeats the process.

For random math, I find it easy to calculate as you go, depending on the program this may or may not be the approach. But this avoids writing 10 individual variables to store each point of data.

So instead, we'll "total as we go" by adding num to ttl.

[SPOILER]
//Make sure your variables have a starting point!
ttl=0

FOR a=1 to 9
num=RND(9)
//This is where the number is added to the total
ttl=ttl+num
//This prints the variable so we can see it
PRINT STR$(num)
NEXT a

//Displays what the total is:
PRINT STR$(ttl)

WAIT KEY
END
[/SPOILER]

Protip: Instead of (ttl=ttl+num) we can use the INC command, which increases it by a value, which can be a variable. So it would be:

INC ttl,num

Dividing is a similar process to the calculated variant. So we would then add these two lines before we "WAIT KEY"

[SPOILER]
//Generate the new number to divide by:
//NOTE: This has to be 8+1 or else a zero could be returned!!!
num=RND(8)+1

ttl=ttl/num
PRINT STR$(ttl)
[/SPOILER]
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 16th Oct 2012 00:49 Edited at: 17th Nov 2012 19:55
Code was offered to the OP, but it was not even acknowledged, so it has been deleted.

So many games to code.....so little time.
Silence
11
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 16th Oct 2012 01:16
ink rgb (0,255,0),0
center text 300,230, "Enter a number (1-5)"
do
If Inkey$()="1" then gosub section1
sync
loop

section1:
REM SCREEN DISPLAY
ink rgb (0,255,0),0
cls 0 : center text 320,240,"Press 'Q' to quit"
runningTotal = 0
for x=1 to 8
newNumber = rnd(8) + 1
inc runningTotal, newNumber
REM this gives 8 values
print "Value #"+Str$ (x)+" = "+Str$(newNumber)
next x

REM this gives you the statement and the random number
center text 320,200,"The sum of the first 8 numbers is " + str$(runningTotal)
print "Raise to the power of "+Str$ (rnd(9)+1)

wait key


This is what I have. I need the 9th value to be divided by the sum of the first 8 values. so it should be like this

Value #1 = (random number)
Value #2 = (random number)
Value #3 = (random number)
Value #4 = (random number)
Value #5 = (random number)
Value #6 = (random number)
Value #7 = (random number)
Value #8 = (random number)
Value #9 = (random number)
Raise to the power of 2

The sum of the first 8 numbers is ________
The first 8 numbers divided by the 9th is _______________
The first 8 numbers divided by the 9th to the power of ___ is _____
Press Q to quit


I got the first part done and need to figure out how to get it to divide and raise to a power. Any ideas?
Mr909
11
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 17th Oct 2012 00:34
I gave you the code for that already.



When you put this after the "NEXT" part of the FOR-NEXT statement, it'll give you the total divided by the number afterward. Here, I'll tweak it a little.



That will print the value of runningTotal/newNumber (or divided by newNumber, in plain English.)

The same is done for powers, as was explained before:



Note that like the others, this only stores the values for the total, the "newNumber", and another variable "power". Your menu code should be formatted to account for what the variable "power" is, as such:



There is a much easier way to modify your menu loop, by getting the codes for that range of numbers and then basing it on an "IF-THEN" using them. I'm not handling it right now but I might show you later.

Would you mind telling me what you're up to? I'm interested and I'm glad you're using my code
Silence
11
Years of Service
User Offline
Joined: 14th Oct 2012
Location:
Posted: 17th Oct 2012 03:28 Edited at: 17th Oct 2012 17:49
Thank you very much everyone!
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 17th Oct 2012 13:28
CODE IN SNIPPETS



OP, this thread looks ridiculous. Come on! Go edit your posts and put the code in snippets. Also, if LBFN gave you some code, you should really be courteous enough to say thank you. I'd appologise if I were you.

Quote: "...make DBPro generate 9 random numbers and add the first 8 divide by the 9th and raise it to a power of another randomly generated number."





Quote: "If I press the number 1 it should generate 9 one digit numbers.
If I press 2 it should generate 9 two digit numbers and continue doing this until the key "5" is pressed where it will generate 9 five digit numbers. Can anyone help me with this?
"


Have g_limit in the previous example modified thusly:

Actual implementation is up to you.

Login to post a reply

Server time is: 2024-04-24 19:53:58
Your offset time is: 2024-04-24 19:53:58