5 Replies - 823 Views - Last Post: 23 May 2012 - 07:48 AM Rate Topic: -----

#1 wd40bomber7  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 22-November 09

Weighted Random Possibilities

Posted 22 May 2012 - 03:30 PM

I am trying to figure out how to implement a function which will take a list of possibilities and a list of weights and spit out a random possibility.

So if I had three possibilities: A, B, C
and I assigned them the weights: .01,.8,.19

From a sample of the functions output, I would expect to see mostly A, a few B, and maybe one C

The language I'm using is both obscure and irrelevant. The tools I have to use are basic arrays, (Not an arraylist or a vector or anything like that) and a GetRandomReal() function. Also the primitive types real,int,bool

Anyone have any ideas? If you want to write an example, I don't care what language its in as long as its fairly popular and not incredibly difficult to understand.

This post has been edited by wd40bomber7: 22 May 2012 - 03:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Weighted Random Possibilities

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,873
  • Joined: 27-December 08

Re: Weighted Random Possibilities

Posted 22 May 2012 - 03:33 PM

If your precision on the probabilities is to the hundreths place, generate randoms from 0-99. So 0 would be associated with A, 1-80 would be for B, and 81-99 would be for C.
Was This Post Helpful? 1
  • +
  • -

#3 wd40bomber7  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 22-November 09

Re: Weighted Random Possibilities

Posted 22 May 2012 - 09:07 PM

That definitely seems like the best option. I don't know why that didn't occur to me right off the bat.

-Thanks
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,873
  • Joined: 27-December 08

Re: Weighted Random Possibilities

Posted 22 May 2012 - 09:08 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#5 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Weighted Random Possibilities

Posted 23 May 2012 - 07:31 AM

Or if you need some more exotic distribution, generate a table with as many similar entries as the proportional share implies.

Then, take a flat distribution random and use the output if it to indicate (calculate) the table index.

That's good way especially if you don't know the distribution, but you can get samples from the real world.

In case you use macosxnerd101's way, remember not to map the seed to the random. It tends to break the random-function logic (pseudo-random sequence).
Use a separate variable.

This post has been edited by turboscrew: 23 May 2012 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: Weighted Random Possibilities

Posted 23 May 2012 - 07:48 AM

Since your function is GetRandomReal, I'd probably keep it real. Assuming the function returns between 0 and 1, and since your weights are already normalized to equal one...

n = GetRandomReal()
if n<0.01:
   A
elif n<0.81:
   B
else:
   C


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1