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%
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 - mydatatype x = new mydatatype();
-
Inside your process loop.
Cheers!
12 8708
I don't know of any built in way, but you could always do something like... - int randVal = Rand.Next(10);
-
int retVal = 0;
-
switch (randVal)
-
{
-
case 0:
-
case 1:
-
retVal = 1;
-
break;
-
case 2:
-
case 3:
-
case 4:
-
case 5:
-
case 6:
-
case 7:
-
retVal = 2;
-
break;
-
case 8:
-
case 9:
-
retVal = 3;
-
break;
-
}
-
-
return retVal;
You'd have to extend that for other numbers :)
Great. Can be used too. I have a try first den I let u know my result. Thx bro ;)
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. - float rValue = Rand(); // should be in the range 0.->1.0
-
-
int iValue;
-
if (rValue < 0.20) iValue = 1;
-
else if (rValue < 0.80) iValue = 2;
-
else iValue = 3;
-
Hopefully the methodology makes sense - the bound on each subsequent if statement is the sum of all probabilities up to that point.
Cheers!
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.
@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.
Statistics was never my thing, a lot of what you said is above me!
:D
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!
Hmmm... I figuring out another way. But seem like couldn't get the result... - using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace ConsoleApplication4
-
{
-
class Program
-
{
-
class mydatatype
-
{
-
public string foodname;
-
public int quantity;
-
}
-
-
public Program() { }
-
-
static void Main(string[] args)
-
{
-
List<mydatatype> mylist = new List<mydatatype>();
-
-
string fdname1, fdname2;
-
-
Random rd = new Random();
-
mydatatype x = new mydatatype();
-
-
for (int i = 0; i < 30; i++)
-
{
-
-
int randome = rd.Next(1, 11);
-
-
if (randome <= 8)
-
{
-
fdname1 = "kampong";
-
x.foodname = fdname1;
-
x.quantity = 1;
-
}
-
else
-
{
-
fdname2 = "chinese";
-
x.foodname = fdname2;
-
x.quantity = 1;
-
}
-
mylist.Add(x);
-
}
-
-
foreach(mydatatype m1 in mylist)
-
{
-
System.Console.WriteLine("Qty:" + m1.quantity + "\tFdname:" + m1.foodname.ToString());
-
}
-
}
-
}
-
}
-
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 - mydatatype x = new mydatatype();
-
Inside your process loop.
Cheers!
Thx @Oralloy
You save me ;)
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
Does that mean that I can enter academic steeplechase competitions, now?
:))
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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
...
|
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.
|
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?
|
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;...
|
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??
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |