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 / Noob question: how to pick random value from an array?

Author
Message
HundredCarWar
14
Years of Service
User Offline
Joined: 28th Aug 2009
Location:
Posted: 29th Aug 2009 01:56
I recently got Dark Basic, so sorry if this is elementary, but I am trying to make a random personality generator for D&D (this is just a text program, no graphics). Here is my incomplete program so far...



I want the program to print a number of random values drawn from the p$ array equal to the variable B (the dice roll). But I am unsure how, (print rnd(p$) didn't work), and right now I am stuck manually 'rerolling' the program. Any help is much appreciated and thanks in advance.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 29th Aug 2009 04:45



TGC Forum - converting error messages into sarcasm since 2002.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Aug 2009 04:53
So, if the die roll is 5, do you want 5 random personality traits printed, or do you want 1 personality trait that is stored in the array at position 5?

For B random traits based on the 4 traits you have coded, you would add a loop to the length of B with repeats:



Enjoy your day.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 29th Aug 2009 04:59
replace those "if A" lines with my line and we've got a deal.

TGC Forum - converting error messages into sarcasm since 2002.
HundredCarWar
14
Years of Service
User Offline
Joined: 28th Aug 2009
Location:
Posted: 29th Aug 2009 07:03
Wow fast replies, thanks guys!

The for next loop did the trick

I did want the die roll (B variable) to determine the amount of personality points, not their position in the p array.

One thing though: I coded B as rnd(6)+1 in the hopes that it would produce random numbers ranging from 2 to 7, but now I realize it doesn't.
So the question is: what should i do to make a 'die roll' that ranges from 2 (minimum) to 7 (maximum).
Mathamatically, you'd think rnd(6)+1 would do it, but that is effectively rnd(7).
This code...

doesn't work either for some reason (it still returns 1 when I replaced B with C in my original code)

Thanks again, this is very helpful!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Aug 2009 10:50 Edited at: 29th Aug 2009 11:27
Quote: "I recently got Dark Basic,"

Hello, and welcome! Should have said that first.

For random values in a certain range, the high value of the range is the the value inside the () plus the outside value. The low value for the range is the outside value.

So, for a function call like rnd(5), the high value is 5 plus an outside value of 0. The low range is the implied outside value of 0. So the range of rnd(5) is 0 to 5 .

How about something like rnd(10)-4 ?

The high value is 10 plus the outside value of -4 :
10 + -4 = 6

The low value is the outside value :
-4

So the range of random values is -4 to 6 .

Put it in reverse to solve for the value in the (). Say you want the high range to be 6 and the low range to -4 but you don't know what value to put in rnd(). Start with what you know:
high value = inside () + outside ()
6 = x + -4
a little algebra to juggle things around:
x = 6 + 4
x = 10
So the function becomes rnd(10)-4

How can you apply this to return the values you want of 2 to 7? If you need help you can look at the solution in the code box below:



And Obese was saying you don't need the conditional if then tests in



It would be much simpler to just:


or even


Enjoy your day.

Login to post a reply

Server time is: 2024-05-09 20:23:27
Your offset time is: 2024-05-09 20:23:27