Click here to Skip to main content
15,887,683 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionRe: generating random number Pin
fmzl21-Dec-10 12:31
fmzl21-Dec-10 12:31 
AnswerRe: generating random number Pin
Jeff Connelly22-Dec-10 3:59
Jeff Connelly22-Dec-10 3:59 
GeneralRe: generating random number Pin
fmzl22-Dec-10 10:52
fmzl22-Dec-10 10:52 
GeneralRe: generating random number Pin
Jeff Connelly27-Dec-10 4:57
Jeff Connelly27-Dec-10 4:57 
QuestionRe: generating random number Pin
NickHighIQ3-Oct-10 13:37
NickHighIQ3-Oct-10 13:37 
AnswerRe: generating random number Pin
fmzl3-Oct-10 15:07
fmzl3-Oct-10 15:07 
GeneralRe: generating random number Pin
Luc Pattyn3-Oct-10 15:21
sitebuilderLuc Pattyn3-Oct-10 15:21 
GeneralRe: generating random number Pin
NickHighIQ3-Oct-10 17:02
NickHighIQ3-Oct-10 17:02 
"option b is impossible, unless the number of runs is a multiple of 10" - then it's not really impossible, is it? Laugh | :laugh:

@fzml
Option A:

Ok, for Option A, Luc's solution is what I'd go with (in fact I can't think of another way off the top of my head, at least one that's not contrived). Assume we have two sets of numbers, A and B. We want 30% chance of selecting something from A, and 70% chance of selecting something from B: in pseudo-C#, something like:

C#
  1  Random rnd = new Random();
  2  
  3  int selector = rnd.Next(1, 10);
  4  
  5  if (selector <= 3) { // select something from the A group }
  6  else { // select something from the B group }


As you can see, the rnd.Next has 10 possible outputs (1, 2, 3 ... 10) - we assign 30% of those, i.e. 1 through 3, to our 30% group. The rest make up the 70% for the other group.

If we wanted 3 groups with (A: 20%, B: 50%, C: 30%):

C#
  1  Random rnd = new Random();
  2  
  3  int selector = rnd.Next(1, 10);
  4  
  5  if (selector in {1, 2}) { // select something from the A group }
  6  else if (selector in {3, 4, 5, 6, 7}) { // select something from the B group
  7  else { // select something from the C group }


By splitting a known number of outcomes up like this, you can have "pre-determined probabilities". Of course, this all relies on the soundness of the Random generator you're using. If you would like to write your own, there's heaps of literature out there on the web about RNGs (check out the Mersenne Twister).

Option B:

Well, Option B (forgive me if I get the wrong terminology here) is about creating a pseudo-random number generator, with period of x, that selects members randomly from different sets based on enforced probabilities.

Ok, well let's say you have two sets A and B:

C#
  1  A = { 1, 2, 3, 4 } // we want EXACTLY 30% of our answers to come from here
  2  B = { 5, 6, 7, 8 } // we want EXACTLY 70% of our answers to come from here


You need to set a "period", that is, what number of interations will satisfy the exactness of our probabilities (for example, up until now we have assumed 100 iterations will satisfay the probabilities).

So, let's say we pick 100 - that means every 100 iterations, we can look at our results and say for sure that 30% of these will be from A and the rest from B.

A very simple way, based on Option A, would be (pseudo-code):

1. Generate a list of numbers, 1 through 100
2. [[Generate a random integer between 1 and 100]] to select one of these numbers
3. If the random number is not in our list
    a) [[Get a number that IS in the list]]
4. If the number we selected is in range 1  - 30, then [[select one from group A]]
5. If the number we selected is in range 31 - 100, then [[select one from group B]]
6. Remove the number from our list!
7. If the list is empty, regenerate it with 1 through 100


Ok, so anything wrapped in square brackets ([[ and ]]) is to indicate that you can do those parts however you want, i.e. it does not matter how you generate your random numbers or how you select from each group etc. This does not affect your requirements.

Anyway, Option B is essentially using the same technique as Option A - use a random number as a way to SELECT a result, rather than as the result itself.

E-mail or reply if you're still lost, I'll try and knock up some code for you to demonstrate better.
GeneralRe: generating random number Pin
fmzl3-Oct-10 17:12
fmzl3-Oct-10 17:12 
GeneralRe: generating random number Pin
NickHighIQ3-Oct-10 17:23
NickHighIQ3-Oct-10 17:23 
GeneralRe: generating random number Pin
Luc Pattyn3-Oct-10 17:22
sitebuilderLuc Pattyn3-Oct-10 17:22 
QuestionRe: generating random number Pin
NickHighIQ3-Oct-10 17:26
NickHighIQ3-Oct-10 17:26 
AnswerRe: generating random number Pin
Luc Pattyn3-Oct-10 17:31
sitebuilderLuc Pattyn3-Oct-10 17:31 
GeneralRe: generating random number Pin
NickHighIQ3-Oct-10 18:15
NickHighIQ3-Oct-10 18:15 
GeneralRe: generating random number Pin
fmzl3-Oct-10 19:08
fmzl3-Oct-10 19:08 
GeneralRe: generating random number Pin
PravinSingh14-Oct-10 10:31
PravinSingh14-Oct-10 10:31 
AnswerRe: generating random number Pin
fmzl14-Oct-10 10:46
fmzl14-Oct-10 10:46 
GeneralRe: generating random number Pin
PravinSingh14-Oct-10 19:47
PravinSingh14-Oct-10 19:47 
QuestionRe: generating random number Pin
AspDotNetDev3-Nov-10 7:24
protectorAspDotNetDev3-Nov-10 7:24 
GeneralRe: generating random number Pin
NickHighIQ3-Oct-10 17:03
NickHighIQ3-Oct-10 17:03 
AnswerRe: generating random number Pin
T210213-Dec-10 21:08
T210213-Dec-10 21:08 
QuestionGoal finding: I just need the name of an algorithm Pin
Quake2Player24-Sep-10 13:19
Quake2Player24-Sep-10 13:19 
AnswerRe: Goal finding: I just need the name of an algorithm Pin
NickHighIQ3-Oct-10 13:46
NickHighIQ3-Oct-10 13:46 
AnswerRe: Goal finding: I just need the name of an algorithm Pin
Radhakrishnan G.3-Nov-10 5:15
Radhakrishnan G.3-Nov-10 5:15 
AnswerRe: How does High Def Streaming Work Pin
Luc Pattyn31-Aug-10 10:14
sitebuilderLuc Pattyn31-Aug-10 10:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.