Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random methods
#1
i need a random number with a bell curve between A and B with absolute confinement to that range. this would be a float value. i have a use case where i need a script to sleep a random amount of time between 20 and 24 minutes with a bell curve centered a 22 minutes. i may need to tweak these values. i can see how to get a confined range when getting a flat uniform "curve". but i need the bell curve and still absolutely confined. can someone explain how to accomplish this with module random?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Can't you set the standard deviation, sigma, appropriately? 99.7% of the values drawn from a Normal distribution fall within +- 3 sigma of the mean (see, e.g. https://en.m.wikipedia.org/wiki/68%E2%80...399.7_rule). random.normalvariate lets you specify both the mean and standard deviation.
Gribouillis likes this post
Reply
#3
import random

def sleeping_time(mu, sigma, min, max):
    while True:
        t = random.normalvariate(mu, sigma)
        if min <= t <= max:
            return t

time = sleeping_time(22 * 60,  30, 20 * 60, 24 * 60)
print(time)
This is rejection sampling with the Bell curve. Of course this is justified only for non ridiculous values of the parameters.

Why do you need a Bell curve? Why do you need it between 22 and 24 minutes?
ndc85430 likes this post
Reply
#4
the need is to disguise that the timing is automated, to make it appear to be somewhat a natural effect of noise.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
i could simulate a bell curve by adding N uniform random values between 0 and 1 and dividing by N and then scaling to my needs. the true bell curve is N == infinity, but values beyond 5 start to appear true. this is about every 20 minutes, so computation time is not an issue for a fairly large N.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Sep-02-2022, 05:39 PM)Skaperen Wrote: i could simulate a bell curve by adding N uniform random values between 0 and 1 and dividing by N and then scaling to my needs.
Why would you do that when you have random.normalvariate() ?
Reply
#7
(Sep-02-2022, 07:38 PM)Gribouillis Wrote:
(Sep-02-2022, 05:39 PM)Skaperen Wrote: i could simulate a bell curve by adding N uniform random values between 0 and 1 and dividing by N and then scaling to my needs.
Why would you do that when you have random.normalvariate() ?
that method does not have a bounds limit.

if i use 240 for N then my pseudo bell curve would have bounds that are 240 apart making these random values just right to be added to the number of seconds to the next 20 minute cycle to get the time i want it to sleep. actually, my zzz() function will be carrying out most of this, being passed the random value as offset and 1200 as the cycle period.
# -*- coding: utf-8 -*-
import time

def zzz(offset=0,cycle=0,minimum=0):
    """Sleep to the next offset time in a time cycle (seconds in float|int).

Copyright © 2022, by Phil D. Howard - all rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
11054987560151472272755686915985840251291393453694611309
(provu igi la numeron al duuma)
"""
    # arg 1: offset or how long to sleep (float or int or decimal.Decimal)
    # arg 2: cycle or period duration    (float or int or decimal.Decimal)
    # arg 3: minimum time to sleep       (float or int or decimal.Decimal)
    # return how many seconds actually slept
    # example:              sleep for 10 minutes: zzz(600)
    # example: sleep to 10 minutes past the hour: zzz(600,3600)
    # example: like above but at least 5 minutes: zzz(600,3600,300)
    # example:            sleep to the next noon: zzz(43200,86400)
    # negative values are not supported

    if offset<0 or cycle<0 or minimum<0:
        raise ValueError('negative values are not supported')
    if cycle:
        offset = (offset-time.time())%cycle
        while offset < minimum:
            offset += cycle
    else:
        if offset < minimum:
            offset = minimum
    if offset > 0:
        time.sleep(offset)
    return offset
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Sep-02-2022, 10:27 PM)Skaperen Wrote: if i use 240 for N then my pseudo bell curve would have bounds that are 240 apart making these random values just right
That's because you don't really understand the central limit theorem. When you sum N random numbers equidistributed in [0, 1], the mean value is N/2 which is 240/2 = 120 sec = 2 minutes in your case, but the standard deviation is sqrt(N/12) which is sqrt(240/12) = 4.5 seconds in your case. It means that you'll find a random waiting time of 22 minutes with a standard deviation of 4.5 seconds, probably not what you want.

With my code, you can choose the standard deviation. Of course, the law is not exactly a Bell curve, it is a Bell curve truncated to the intervall [20, 24], but as @ndc85430 said above, if the standard deviation is not too large, it amounts to approximately the same thing.
ndc85430 likes this post
Reply
#9
the mean of 22 minutes (1320 seconds) was what i originally wanted. thinking about that more, i should go for a mean of 20 minutes (1200 seconds). that and i can have a wider range, which N=480 which would give me a range of 0 to 8 minutes (0 to 480 seconds) to be added to 16 minutes (960 seconds) to get the mean of 20m (1320s).

i don't really care that much about the standard deviation. i have operational concerns. i need to confine these times so that the process will operate correctly. it needs to wake up exactly once near the end of every 20 minute period. a theoretical bell curve has an infinite range. that's not what i want to be using. but, i want it to look about like i am using that.

Wall
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
what would be useful is a way to output raw pixel graphics into the text of my terminal window. but, that is getting off topic and needs to talk in the bar if anyone does want to talk about it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help on random methods jip31 2 1,980 Apr-29-2021, 12:15 PM
Last Post: jip31

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020