DND dice roller

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
BellDynJinn
Posts: 6
Joined: Fri Nov 19, 2021 10:58 am
Has thanked: 1 time
Contact:

DND dice roller

Post by BellDynJinn »

Hi, i am programming a DND dice roller for a college project but can't seem to get the random number generator part, used to simulate a dice roll, to work. could someone take a look at what i have done so far and try to get it to work.
i will be working on this while i wait to see if anyone can help and if i can get it to work before someone can help me, ill edit this post below to show that i havew resolved it.

current state: seemingly resolved.
Attachments
Dnd Dice Roller.fcfx
(20.79 KiB) Downloaded 3 times
Last edited by BellDynJinn on Fri Nov 26, 2021 10:47 am, edited 1 time in total.

medelec35
Valued Contributor
Valued Contributor
Posts: 9390
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2565 times
Been thanked: 3780 times
Contact:

Re: DND dice roller

Post by medelec35 »

Hi, BellDynJinn.
Welcome to the forums.
You can use the Random() function within a calculation icon, no need for it to be contained within a C code block
For example, you can use the random in the following way:
Random Number.png
Random Number.png (6.37 KiB) Viewed 191 times
The % means modulus.
Note the random values are pseudo which means they are is statistically random, but it is derived from a known starting point.
So every time power is applied, the same random numbers will be derived.

There is a way around that.
The stand function can be used.
What I do is Add an available ADC channel, and leave the pin floating.
Assing a variable e.g

Code: Select all

ADCValue
.
Then use a c code block before the main loop with:

Code: Select all

srand(FCV_ADCVALUE);
Martin

BellDynJinn
Posts: 6
Joined: Fri Nov 19, 2021 10:58 am
Has thanked: 1 time
Contact:

Re: DND dice roller

Post by BellDynJinn »

its the Random function itself that isnt working, i cant get it to generate a number other than 0.

medelec35
Valued Contributor
Valued Contributor
Posts: 9390
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2565 times
Been thanked: 3780 times
Contact:

Re: DND dice roller

Post by medelec35 »

Have you tried what I have shown within the image?
That should give you a random number between the min and max values.
If you have and still not working, then post our updated flowchart, please.
Don't forget to enter

Code: Select all

RandNum = random () % (MaxNum - MinNum + 1) + MinNum
within a calculation icon:
Calculation Icon.png
Calculation Icon.png (2.3 KiB) Viewed 137 times
Martin

BellDynJinn
Posts: 6
Joined: Fri Nov 19, 2021 10:58 am
Has thanked: 1 time
Contact:

Re: DND dice roller

Post by BellDynJinn »

when i have tried, in the past and just now, to do it through the calculation method(or the way you have suggested) it always has 0 as the result.

BellDynJinn
Posts: 6
Joined: Fri Nov 19, 2021 10:58 am
Has thanked: 1 time
Contact:

Re: DND dice roller

Post by BellDynJinn »

heres the updated file
Attachments
Dnd Dice Roller.fcfx
(20.77 KiB) Downloaded 4 times

medelec35
Valued Contributor
Valued Contributor
Posts: 9390
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2565 times
Been thanked: 3780 times
Contact:

Re: DND dice roller

Post by medelec35 »

That is because you are calling a user macro (D4) and expecting a return value.
Within the call user macro, you must specify

Code: Select all

.Return
The

Code: Select all

.
means it's a local variable.
Just select the Locals icon and drag

Code: Select all

Return
or just type it in.
Your calculation should look like this:
Use Return.png
Use Return.png (41.34 KiB) Viewed 95 times
Martin

BellDynJinn
Posts: 6
Joined: Fri Nov 19, 2021 10:58 am
Has thanked: 1 time
Contact:

Re: DND dice roller

Post by BellDynJinn »

ok i will try that later today when i have access to flowcode.

BellDynJinn
Posts: 6
Joined: Fri Nov 19, 2021 10:58 am
Has thanked: 1 time
Contact:

Re: DND dice roller

Post by BellDynJinn »

medelec35 wrote:
Wed Nov 24, 2021 2:53 pm
That is because you are calling a user macro (D4) and expecting a return value.
Within the call user macro, you must specify

Code: Select all

.Return
The

Code: Select all

.
means it's a local variable.
Just select the Locals icon and drag

Code: Select all

Return
or just type it in.
Your calculation should look like this:Use Return.png
that seems to be working now thank you

medelec35
Valued Contributor
Valued Contributor
Posts: 9390
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2565 times
Been thanked: 3780 times
Contact:

Re: DND dice roller

Post by medelec35 »

You're welcome.
Thanks for letting us know.
Martin

Post Reply