Changing the the bounds for base.rand Float outputs

Hey new to julia and wondering how to augment the bounds of the base.rand() function for Float64 numbers, I’m trying to get (-1,1) as the new min and max.

I checked the documentation on this page and it isn’t clear to me how to change typemin(S):typemax(S) for an individual usage of the function.

Thank you so much for any assistance and apologies if this is already a commonly asked question, I couldn’t find anyone who asked something similar.

Rather than changing typemin / typemax which affect more than rand(), it’s much safer to define a myrand() = rand() * 2 - 1 function which gives you the range you want without further effects.

3 Likes

Do note that this produces values in [-1, 1). So if @ApproximateTruth wants exclusive bounds (-1, 1), then they can do rand() * 2 - prevfloat(1.0). Though practically this makes no difference since the chance of randomly getting exactly -1 is so small :yum:

1 Like

Thank you for your reply, I was thinking of just scaling it but was wondering it there was a pre-existing way of augmenting the bounds that I was just missing.

Much appreciated

You could use rand(Uniform(-1,1)) via Distributions.jl.

1 Like