Hi.
How can I generate a random real number between -3 and 3 that is exclusive on both sides?
Thank you.
rand()*(3.0 - (-3.0)) + (-3.0)
That doesn’t exclude -3.0
, because rand()
is uniform in [0,1)
.
See this answer: How to create a random Uniform Distribution between (but excluding) 0 and 10? - #21 by Elrod, which leads to the analogous solution:
prevfloat(6.0) * (1 - rand()) - 3
for generating random numbers uniformly for floating-point values in the open interval (-3,3)
.
Thank you, Marc!
That would have taken me at least forever to figure out.
Gonna go with Steven’s solution; thank you nonetheless!
Thank you, Steven.
I didn’t see your answer before thanking Marc for a solution, lol.
I’m going to go with your solution on faith for now, until I am more adept with Julia.
Thank you.
EDIT: Actually, I recall reading about prevfloat() somewhere and your solution makes clear sense now.
Thanks again.
How to get random number between
small < x < large
julia> small=-3.0
-3.0
julia> large=3.0
3.0
julia> prevfloat(large - small) * (1 - rand()) + small
-0.10856055483983429
Thank you, StevenSiew.
From stevengj’s answer, my perspective was: excluding the minimum via (1-rand()) and excluding the maximum via prevfloat(6.0) and then adjusting the final output by subtracting the midway value.
However I do appreciate your formulaic presentation for it’s clarity, so I will mark your response as the solution for future seekers…