Random Integer from (-1.0 -> +1.0)
-
I need to randomize a knob from a negative low to a postive high number but I want it to return a float number instead of whole integers. I noticed both of the random api's either do one or the other. Can somebody school me on how to do this pls? Thanks in advance!
-
@Chazrox This is one of the instances where the GPT is useful.
const randomFloat = (Math.random() * 2) - 1; Console.print(randomFloat);
Explanation:
Math.random() generates a number in the range [0, 1].
Multiplying by 2 scales it to [0, 2].
Subtracting 1 shifts it to [-1, 1]. -
@d-healey said in Random Integer from (-1.0 -> +1.0):
Explanation:
Math.random() generates a number in the range [0, 1].
Multiplying by 2 scales it to [0, 2].
Subtracting 1 shifts it to [-1, 1].Thanks for this. That really helps me understand the logic. I get it now. Im gonna watch the other video you gave me and put this together. Thanks again!
-
C Chazrox marked this topic as a question
-
C Chazrox has marked this topic as solved
-
@d-healey I cant figure out what to change.
-
@Chazrox That * doesn't look right, did you copy/paste it from somewhere or are you using a different font?
This is how it looks for me
-
@d-healey I typed it out myself. Its an " * " right?
-
@d-healey I didnt change my font either. Thats default font.
-
@Chazrox What happens when you copy/paste those two lines to a new empty HISE file?
-
@Chazrox What do you have before line 7? sometimes an error ahead can appear later, for instance when a line of code isn't closed
;
-
@d-healey looks the same.
this is your code snippet copy/pasted. -
@Chazrox said in Random Integer from (-1.0 -> +1.0):
looks the same.
Yes, but does it work in the new project?
-
-
@ustk I been changing each one to test but it wasnt working. Trying it in a new snippet per David rn. My lines are closed up above.
-
@Chazrox Since it's a namespace, maybe it's in the main script... The console is definitely saying something about a missing semicolon so...
-
@Chazrox And when you compile, is the focus on the script? meaning not "(detached)". This happens when you edit another script at the same time, so the modifications aren't compiled.
I find myself being tricked a lot by this everyday... "why the heck this isn't working... Oh ok... silly me..." -
@Chazrox Don't use var for variables except when you have no other choice
-
-
@d-healey sorry that pic was cut off. They're const vars for sure. I remember you teaching that in the tutorial.
-
More than just helping me fix the problem. I always appreciate the logic! You guys are the best!
-
@ustk said in Random Integer from (-1.0 -> +1.0):
is the focus on the script?
you mean am I compiling while on the main script?
I havent seen that "detached' yet. My namespaces are local (external scripts) to the project. Would that apply to this situation?
If im working within a namespace i'll usually do both for good measure...atleast I think thats good measure. lol.