Random Number Generation in a given range

4,014 views
Skip to first unread message

Guillermo Estrada

unread,
Mar 27, 2012, 8:13:55 PM3/27/12
to golan...@googlegroups.com
Hi, I've been reading some posts but I still have the same question.

I need to generate a random number between a min and a max value of floats (Float64) for example:

random(-0.001, 0.001)

Is there any way pkg "math/rand" could do this using the Normal Distribution or such? Most Random generation on the pkg works in [0.0,1.0)
The closest I found is:

func (*Rand) NormFloat64

But I need a way to clamp that range into my own, any ideas? Thnx in advance!

Andrew Gerrand

unread,
Mar 27, 2012, 8:28:21 PM3/27/12
to Guillermo Estrada, golan...@googlegroups.com

A normal distribution has no upper or lower bound, so NormFloat64
might not be what you want.

This expression will give you a random float bounded to [-0.001,0.001):

(rand.Float64() * 2 - 1) * 0.001

Andrew

si guy

unread,
Mar 27, 2012, 8:36:33 PM3/27/12
to golan...@googlegroups.com
If you clamp a normal distribution then it isn't a normal distribution anymore... Are you saying that you want [-0.001,0.001] to be within n sigma of a mean of zero?

If that's the case then
MyVal := rand.NormFloat64() * ( 0.001 / n )

You can clamp it afterwards with a switch
switch {
case MyVal > 0.001:
MyVal = 0.001
case MyVal < -0.001:
MyVal = -0.001
}

But again, this won't be a normal distribution anymore...

Guillermo Estrada

unread,
Mar 27, 2012, 8:49:11 PM3/27/12
to golan...@googlegroups.com, Guillermo Estrada

A normal distribution has no upper or lower bound, so NormFloat64
might not be what you want.

This expression will give you a random float bounded to [-0.001,0.001):

    (rand.Float64() * 2 - 1) * 0.001

Andrew


This expression seems pretty nice and compact. It solves my specific problem right now. I have been analyzing it and it works with any range where max min values are on negative and positive sides respectively and are of course the same.

How can it be done to use any arbitrary max min values?

ie. 
random(23.45, 54.8)
random(-1.123, 987.23)
random(-82.12,-2.65)

Of course I know this will need a lot more coding, but I use a lot of random numbers in computer graphic algorithms and it will be best to have a working function.
Thanx a lot Andrew, my brain was kinda dry already :P

Matt Kane's Brain

unread,
Mar 27, 2012, 8:57:58 PM3/27/12
to Guillermo Estrada, golan...@googlegroups.com
This should do it:

func random(min, max float64) float64 {
return rand.Float64() * (max - min) + min
}

On Tue, Mar 27, 2012 at 20:49, Guillermo Estrada <phro...@gmail.com> wrote:
> How can it be done to use any arbitrary max min values?


--
matt kane's brain
http://hydrogenproject.com

Guillermo Estrada

unread,
Mar 27, 2012, 9:08:34 PM3/27/12
to golan...@googlegroups.com, Guillermo Estrada
This should do it:

func random(min, max float64) float64 {
  return rand.Float64() * (max - min) + min
}


It truly does as far as I can tell :D thanks a lot! My brain can rest in peace now! In my road to learning Go, I ported a raytracer (I'm impressed with Go's speed) and now I'm making a web application that generate deJong Attractors using density maps. The more I use Go the more caveats I found, but at the same time the more I like it. And the cream of the crop of this language is truly the community behind it!

alok....@practo.com

unread,
Jul 9, 2017, 3:02:43 PM7/9/17
to golang-nuts
A python like range utility which can be used for this
```random.RangeInt(2, 100, 3)```

peterGo

unread,
Jul 10, 2017, 12:24:23 AM7/10/17
to golang-nuts
Alok,

Your import path "github.com/alok87/goutils/random" is not valid for "go get". The source is at "github.com/alok87/goutils/pkg/random".

You write "for { rand.Seed(); rand.Intn(); }". Therefore, the range of values is not random.

You write "arr[r] = rand.Intn(max) + min" which is incorrect.

And so on.

Peter

Alok Kumar Singh

unread,
Jul 10, 2017, 9:05:21 AM7/10/17
to peterGo, golang-nuts
I have fixed it thanks

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/_M-8hRpQs84/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Regards,

Alok Kumar Singh 


Platform Engineering
+ 91 9880121029 

Sebastien Binet

unread,
Jul 10, 2017, 9:17:08 AM7/10/17
to Alok Kumar Singh, peterGo, golang-nuts
Alok,

On Mon, Jul 10, 2017 at 3:04 PM, Alok Kumar Singh <alok....@practo.com> wrote:
I have fixed it thanks

I wouldn't recommend using rand.Seed as you do: this defeats any attempt at getting reproductible results.

-s

Alok Kumar Singh

unread,
Jul 10, 2017, 10:42:07 AM7/10/17
to Sebastien Binet, peterGo, golang-nuts
in my usecasei use this to generate random slice everytime 
dont want the same result
using it to test my algos

Jan Mercl

unread,
Jul 10, 2017, 10:45:13 AM7/10/17
to Alok Kumar Singh, Sebastien Binet, peterGo, golang-nuts

On Mon, Jul 10, 2017 at 4:41 PM Alok Kumar Singh <alok....@practo.com> wrote:

> in my usecasei use this to generate random slice everytime
> dont want the same result
> using it to test my algos

For that you need one only call to Seed at init().

--

-j

alok....@practo.com

unread,
Jul 10, 2017, 12:05:38 PM7/10/17
to golang-nuts, alok....@practo.com, seb....@gmail.com, go.pe...@gmail.com
Reply all
Reply to author
Forward
0 new messages