473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

random number with probability

Is there any way to make a number that generated by random number is the highest probability among all?

eg.

random no 1 20%
random no 2 (Higher percentage) - 60%
random no 3 20%
Aug 10 '10 #1

✓ answered by Oralloy

Let me guess, you get the same answer for all foods?

It looks like you only have one instance of the variable x, which you refer to in all 30 elements of your list.

Move the definition/assignment
Expand|Select|Wrap|Line Numbers
  1. mydatatype x = new mydatatype(); 
  2.  
Inside your process loop.

Cheers!

12 8708
GaryTexmo
1,501 Expert 1GB
I don't know of any built in way, but you could always do something like...

Expand|Select|Wrap|Line Numbers
  1. int randVal = Rand.Next(10);
  2. int retVal = 0;
  3. switch (randVal)
  4. {
  5.   case 0:
  6.   case 1:
  7.     retVal = 1;
  8.     break;
  9.   case 2:
  10.   case 3:
  11.   case 4:
  12.   case 5:
  13.   case 6:
  14.   case 7:
  15.     retVal = 2;
  16.     break;
  17.   case 8:
  18.   case 9:
  19.     retVal = 3;
  20.     break;
  21. }
  22.  
  23. return retVal;
You'd have to extend that for other numbers :)
Aug 10 '10 #2
Great. Can be used too. I have a try first den I let u know my result. Thx bro ;)
Aug 10 '10 #3
Oralloy
985 Expert 512MB
I'm not a c# kind of guy, but the code @garyTexmo wrote is a little bit difficult to implement for the general case.

In my example, I assume that the method/function Rand() returns a floating point value in the range of zero to one, with even distibution.
Expand|Select|Wrap|Line Numbers
  1. float rValue = Rand();   // should be in the range 0.->1.0
  2.  
  3. int iValue;
  4. if (rValue < 0.20)       iValue = 1;
  5. else if (rValue < 0.80)  iValue = 2;
  6. else                     iValue = 3;
  7.  
Hopefully the methodology makes sense - the bound on each subsequent if statement is the sum of all probabilities up to that point.

Cheers!
Aug 10 '10 #4
GaryTexmo
1,501 Expert 1GB
I agree with you, Oralloy, I just wrote it like that to demonstrate the idea of separating the ranges. Your way is definitely more elegant. Though I would advocate the switch and the integer only if you're trying to squeeze as much speed as possible out of your app, which these days is a fairly rare case.

In my head I was envisioning a class where you registered values with probabilities and it would return a value based on that, haha.
Aug 10 '10 #5
Oralloy
985 Expert 512MB
@GaryTexmo

There's no problem with having a class to encapsulate the idea of a non-uniformly distributed random number generator.

The trick is in the implementation. There are some fairly well developed transformations for things like gaussian distributions. But for an arbitrary distribution, it's all fair game.

Get the generator to work right first, then optimize it.

As a side note, optimization is a tricky thing.

Who was it that said 97% of all optimization isn't?

Quips aside, the structure used to implement should reflect the distribution of the data and the value range. If there are 1,000 possibilities, then I'd say we should consider a binary search into a lookup vector. If we were to reproduce a continuum, then I'd say we have to be a little more clever.

However, for three items, I don't see how we could get any smarter than cascading ifs.

If the values were highly skewed to a very few, then a combination of the above might be best. Or maybe a depth optimized look-up tree.

On the other hand your method is good, if the dispatch down that switch was only one instruction/memory fetch. I think the VAX architecture allowed dispatches on a fixed range of integer values of up to 256 elements in one instruction cycle. However I'd hate to try a similar beast on an IBM360.

Oh well, enough mental play - time to get back to work and earn my paycheck.
Aug 11 '10 #6
GaryTexmo
1,501 Expert 1GB
Statistics was never my thing, a lot of what you said is above me!

:D
Aug 11 '10 #7
Oralloy
985 Expert 512MB
Sorry, @GaryTexmo, I didn't mean to get on my academic horse.

Basically I wrote that building a good non-linear random number generator is non-trivial.

Optimizing one is worse.

Cheers!
Aug 11 '10 #8
Hmmm... I figuring out another way. But seem like couldn't get the result...

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication4
  7. {
  8.     class Program
  9.     {
  10.         class mydatatype
  11.         {
  12.             public string foodname;
  13.             public int quantity;
  14.         }
  15.  
  16.         public Program() { }
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             List<mydatatype> mylist = new List<mydatatype>();
  21.  
  22.             string fdname1, fdname2;
  23.  
  24.             Random rd = new Random();
  25.             mydatatype x = new mydatatype();
  26.  
  27.             for (int i = 0; i < 30; i++)
  28.             {
  29.  
  30.                 int randome = rd.Next(1, 11);
  31.  
  32.                 if (randome <= 8)
  33.                 {
  34.                     fdname1 = "kampong";
  35.                     x.foodname = fdname1;
  36.                     x.quantity = 1;
  37.                 }
  38.                 else
  39.                 {
  40.                     fdname2 = "chinese";
  41.                     x.foodname = fdname2;
  42.                     x.quantity = 1;
  43.                 }
  44.                 mylist.Add(x);
  45.             }
  46.  
  47.             foreach(mydatatype m1 in mylist)
  48.             {
  49.                 System.Console.WriteLine("Qty:" + m1.quantity + "\tFdname:" + m1.foodname.ToString());
  50.             }
  51.         }
  52.     }
  53. }
  54.  
Aug 11 '10 #9
Oralloy
985 Expert 512MB
Let me guess, you get the same answer for all foods?

It looks like you only have one instance of the variable x, which you refer to in all 30 elements of your list.

Move the definition/assignment
Expand|Select|Wrap|Line Numbers
  1. mydatatype x = new mydatatype(); 
  2.  
Inside your process loop.

Cheers!
Aug 11 '10 #10
Thx @Oralloy
You save me ;)
Aug 11 '10 #11
GaryTexmo
1,501 Expert 1GB
Oh, no offense was taken and feel free to get on your academic high horse as much as you want! I'm just saying you went above me haha :P
Aug 11 '10 #12
Oralloy
985 Expert 512MB
Does that mean that I can enter academic steeplechase competitions, now?

:))
Aug 11 '10 #13

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

Similar topics

4
by: james blair | last post by:
Hi I am generating a random number using random.randint(1,10000000000) Whats the possibility that the numbers generated will be same when generated by 100 users at the same time? Whats the best...
5
by: drs | last post by:
Is there any way to generate random numbers based on arbitrary real valued functions? I am looking for something like random.gauss() but with natural log and exponential functions. thanks, -d
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
15
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe ...
8
by: asdf | last post by:
I want a random number generator, the random number should be subject a uniform distribution in . Could you please give me some hints? Thanks.
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
19
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.