Author Topic: trying to get random numbers.  (Read 2402 times)

Taiphoz

  • Newbie
  • *
  • Posts: 2
trying to get random numbers.
« on: December 30, 2020, 07:37:13 AM »
So trying to do the following in the screen shot bellow.

Say "Roll (somenumber)" and have it log the number it gets and read the number back to me.
I managed to get it to do like a normal 6 dot roll but I am having trouble getting it to give me a random sized dice roll.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: trying to get random numbers.
« Reply #1 on: December 30, 2020, 10:19:35 AM »
The "{RANDOM:}" token takes two parameters, just like the "A random value" option of the "Set an Integer Value" action.

In addition, you don't need to convert the integer variable value to a text variable in order to output it as text, as  tokens always output text; Instead, use the appropriate token for the variable type (E.G. "{INT:}", in this case)

All of this can be found in the documentation; Press F1 while VoiceAttack has focus to open VoiceAttackHelp.pdf in your default PDF viewer.


These topics may also be of use:
Control flow (If, Else, ElseIf, Loop, Jump) basics
Variables and tokens summed up

Taiphoz

  • Newbie
  • *
  • Posts: 2
Re: trying to get random numbers.
« Reply #2 on: December 30, 2020, 10:37:20 AM »
how do I get a variable into the random token tho ?

I tried this {RANDOM:1:{CMDSEGMENT:1}} to try and get a random number from 1 to what ever number i speak but it didnt work.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: trying to get random numbers.
« Reply #3 on: December 30, 2020, 10:43:01 AM »
Have you read the documentation, or the second linked topic? Tokens are not variables.

If you want to set an integer variable to the output of a token (which, again, is always literal text), you need to use the "Convert Text/Token" field (note that it mentions "Token" specifically), not the "Another Variable" field.


If you're not doing math or storing the value for later, you don't need to involve variables at all, as you can output directly to the log using the "Write a Value to the Event Log" action, or have it spoken by the "Say Something with Text-To-Speech" action.

priscalos

  • Newbie
  • *
  • Posts: 2
Re: trying to get random numbers.
« Reply #4 on: May 31, 2023, 06:32:38 AM »
Has anyone tried using the Math.random() function?

dominicyberx

  • Newbie
  • *
  • Posts: 1
Re: trying to get random numbers.
« Reply #5 on: May 31, 2023, 07:54:59 AM »
To get a specific range, you can multiply the result by the maximum number you want and then round it down using Math.floor(). Don't forget to add 1 to the result to ensure you include the minimum value.
For example, if you're looking to simulate a dice roll from 1 to 6, you can use Math.floor(Math.random() * 6) + 1. This expression will give you a random whole number between 1 and 6.
To display the number, you can use console.log() to print it in the console. As for reading it back, you can explore text-to-speech libraries or browser APIs that can assist with converting the number into audible speech.
By the way, if you ever need a quick way to test random number generation, you can check out the Dice Roller at https://flipsimu.com/dice-roller/roll-d12/. It's a handy tool for generating random dice rolls virtually.
« Last Edit: June 02, 2023, 10:16:46 AM by dominicyberx »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: trying to get random numbers.
« Reply #6 on: May 31, 2023, 08:04:23 AM »
Has anyone tried using the Math.random() function?

The following native features are specifically intended to generate random numbers:
The "{RANDOM:}" token
The "{RANDOMDEC:}" token, for non-integer values
The "A random value" option of the "Set an Integer Value" action
The "A random value" option of the "Set a Decimal Value" action, for non-integer values
(and technically the "A random value" option of the "Set a Small Integer Value" action, however that datatype is deprecated)

I'm curious as to why you're seemingly opting to use a lower level method in an inline function (or plugin, I suppose), rather than those native features.
Do none of them fit your use case?