DND dice roller
Moderator: Benj
-
- Posts: 6
- Joined: Fri Nov 19, 2021 10:58 am
- Has thanked: 1 time
DND dice roller
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.
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 210 times
Last edited by BellDynJinn on Fri Nov 26, 2021 10:47 am, edited 1 time in total.
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: DND dice roller
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: 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.
Then use a c code block before the main loop with:
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: 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
-
- Posts: 6
- Joined: Fri Nov 19, 2021 10:58 am
- Has thanked: 1 time
Re: DND dice roller
its the Random function itself that isnt working, i cant get it to generate a number other than 0.
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: DND dice roller
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 within a calculation icon:
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
Martin
-
- Posts: 6
- Joined: Fri Nov 19, 2021 10:58 am
- Has thanked: 1 time
Re: DND dice roller
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.
-
- Posts: 6
- Joined: Fri Nov 19, 2021 10:58 am
- Has thanked: 1 time
Re: DND dice roller
heres the updated file
- Attachments
-
- Dnd Dice Roller.fcfx
- (20.77 KiB) Downloaded 248 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: DND dice roller
That is because you are calling a user macro (D4) and expecting a return value.
Within the call user macro, you must specify
The means it's a local variable.
Just select the Locals icon and dragor just type it in.
Your calculation should look like this:
Within the call user macro, you must specify
Code: Select all
.Return
Code: Select all
.
Just select the Locals icon and drag
Code: Select all
Return
Your calculation should look like this:
Martin
-
- Posts: 6
- Joined: Fri Nov 19, 2021 10:58 am
- Has thanked: 1 time
-
- Posts: 6
- Joined: Fri Nov 19, 2021 10:58 am
- Has thanked: 1 time
Re: DND dice roller
that seems to be working now thank youmedelec35 wrote: ↑Wed Nov 24, 2021 2:53 pmThat is because you are calling a user macro (D4) and expecting a return value.
Within the call user macro, you must specifyTheCode: Select all
.Return
means it's a local variable.Code: Select all
.
Just select the Locals icon and dragor just type it in.Code: Select all
Return
Your calculation should look like this:Use Return.png
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times