Hey
I am trying to figure out a very simple code to do some weighted randomness.
I have two values, and I have to randomly pick among them on each iteration.
But I need, let's say, the first value to be picked on 60% of the cases, and the second value to be picked on 40% of the cases.
Anyone can write me some code or even pseudo code to do that?
I know it is simple, I learned it one day in my life, but right now I'm blank!
Thank u a lot
Daniel
4 1599
Hey
I am trying to figure out a very simple code to do some weighted randomness.
I have two values, and I have to randomly pick among them on each iteration.
But I need, let's say, the first value to be picked on 60% of the cases, and the second value to be picked on 40% of the cases.
Anyone can write me some code or even pseudo code to do that?
I know it is simple, I learned it one day in my life, but right now I'm blank!
Thank u a lot
Daniel
I'm more of a mechanic than a mathematician (gui & device interfaces), but there sure are a lot of interesting looking functions with names like paretovariate in the random module. One of them has just gotta do what you're look for.
Here's an ugly way to do it: create a list of ten with 6 of one value and 4 of another, than randomly access the list. -
import random
-
import math
-
-
s = [6,6,6,6,6,6,4,4,4,4]
-
-
for i in range(1,100):
-
print s[ int( math.floor( random.random() * 10) ) ]
-
Here's an ugly way to do it: create a list of ten with 6 of one value and 4 of another, than randomly access the list. -
import random
-
import math
-
-
s = [6,6,6,6,6,6,4,4,4,4]
-
-
for i in range(1,100):
-
print s[ int( math.floor( random.random() * 10) ) ]
-
Just a little note: you don't need all that math to get a value from 1 to ten. Just do this: -
print s[random.randint(0, 9)]
-
No biggie :)
Thanks, I don't know how I missed that. I've just started playing with Python and will remember it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: HEMH6 |
last post by:
Weihted Average
Write a C program to calculate and print the weighted average of a list of N
floating point number, using the formula
Xave = F1X1 + F2X2+...+ FnXn
where the F's are...
|
by: Salad |
last post by:
http://www.mathwords.com/w/weighted_average.htm
At the above link gives an example of a weighted average. It uses the
following example:
Grades are often computed using a weighted average....
|
by: alessandro.carrega |
last post by:
In Igraph library (http://cneurocvs.rmki.kfki.hu/igraph/) there's a
method to add a weighted edge?
|
by: edmund_xue |
last post by:
Hello There,
I was just working on a project of creating a database for a relative that
have alots of clients. His company managed the shares portfolio of clients
and he requires the buy price...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |