472,992 Members | 3,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Weighted randomness

1
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
Jul 10 '07 #1
4 1599
bartonc
6,596 Expert 4TB
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.
Jul 10 '07 #2
dmr
4
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.
Expand|Select|Wrap|Line Numbers
  1. import random
  2. import math
  3.  
  4. s = [6,6,6,6,6,6,4,4,4,4]
  5.  
  6. for i in range(1,100):
  7.   print s[ int( math.floor( random.random() * 10) ) ]
  8.  
Aug 2 '07 #3
ilikepython
844 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. import random
  2. import math
  3.  
  4. s = [6,6,6,6,6,6,4,4,4,4]
  5.  
  6. for i in range(1,100):
  7.   print s[ int( math.floor( random.random() * 10) ) ]
  8.  
Just a little note: you don't need all that math to get a value from 1 to ten. Just do this:
Expand|Select|Wrap|Line Numbers
  1. print s[random.randint(0, 9)]
  2.  
No biggie :)
Aug 2 '07 #4
dmr
4
Thanks, I don't know how I missed that. I've just started playing with Python and will remember it.
Aug 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
3
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....
6
by: alessandro.carrega | last post by:
In Igraph library (http://cneurocvs.rmki.kfki.hu/igraph/) there's a method to add a weighted edge?
0
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...
0
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=()=>{
2
isladogs
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...
0
tracyyun
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...
4
NeoPa
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 :...
0
isladogs
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...
0
NeoPa
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...
0
isladogs
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...
4
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...
3
SueHopson
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.