I am using C# RNGCryptoServiceProvider inside a using block
byte[] values = new byte[ammountToCreate];
using (RNGCryptoServiceProvider randomizer = new RNGCryptoServiceProvider())
{
randomizer.GetBytes(values);
}
The result values are then normalized to interval [0:15], therefore no modulo bias should affect the output.
Normalization is done in this way:
byte n = (byte)(this.maxRandomValue + 1);
// loop all element in the input value, an apply modulo.
for (int i = 0; i < values.Length; i++)
{
values[i] = (byte)(values[i] % n);
}
Where maxRandomValue is equal to 15.
However, from the first test, the output seems to be not uniform.
I have generated 2916 numbers, and this is the values distribution:
+-------+--------------+
| Value | % On Total |
+-------+--------------+
| 0 | 6.52% |
| 1 | 5.90% |
| 2 | 6.10% |
| 3 | 6.34% |
| 4 | 5.73% |
| 5 | 6.89% |
| 6 | 5.49% |
| 7 | 6.86% |
| 8 | 5.66% |
| 9 | 5.97% |
| 10 | 6.58% |
| 11 | 6.04% |
| 12 | 6.48% |
| 13 | 5.97% |
| 14 | 7.17% |
| 15 | 6.31% |
+-------+--------------+
As you can see:
6 --> 5.49% of generated numbers
14 --> 7.17% of generated numbers
My fear is that maybe I have generated just few numbers and with bigger volumes the distribution becomes uniform.
Or, RNGCryptoServiceProvider inside a using is not working as expected.
Do you have any idea?
Related
Have two tables:
Catalog
|----------|----------|-----------|
| Id | Name | CreatedBy |
|----------|----------|-----------|
| 1 | Catalog1 | 1 |
| 2 | Catalog2 | 1 |
| 3 | Catalog3 | 1 |
| 4 | Catalog4 | 2 |
|----------|----------|-----------|
TemplateOnCatalog
|------------|-----------|
| TemplateId | CatalogId |
|------------|-----------|
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 4 |
|------------|-----------|
Using c# and Linq, how to get a list of all the catalogs, where CreatedBy = 1, and count the number of templates in the catalog. If no templates, the count value should be 0 (or empty).
Expected result:
|------------|-------------|-------------------|
| CatalogId | CatalogName | NumberOfTemplates |
|------------|-------------|-------------------|
| 1 | Catalog1 | 2 |
| 2 | Catalog2 | 3 |
| 3 | Catalog3 | 0 |
|------------|-------------|-------------------|
I try the following, but it doesn't take catalogs without templates:
var templateCatalogs =
from templateCatalog in db.Catalog.AsNoTracking()
join totc in db.TemplateOnCatalog
on templateCatalog.Id equals totc.CatalogId
where
templateCatalog.CreatedBy == 1
orderby templateCatalog.Name
group new { templateCatalog, totc }
by new { templateCatalog.Name, templateCatalog.Id } into result
select new
{
CatalogId = result.Key.Id,
CatalogName = result.Key.Name,
NumberOfTemplates = result.Count()
};
I try to add new row to footer of column at calculated for Average of each cell in column.
Original datatable:
col1 | col2 | col3 | ... | col20 |
------|------|------|-----|-------|
5 | 10 | 5 | ... | 8 |
------|------|------|-----|-------|
8 | 7 | 8 | ... | 2 |
And I would like to do below :
col1 | col2 | col3 | ... | col20 |
------|------|------|-----|-------|
5 | 10 | 5 | ... | 8 |
------|------|------|-----|-------|
8 | 7 | 8 | ... | 2 |
-------|------|------|-----|-------|
6.5 | 8.5 | 6.5 | ... | 5 |
Here's my code:
if(sum[i] == 0 )
{
dt.Columns.Remove("BIN"+i.ToString()+"");
}
else
{
dt.Columns.Add("%BIN"+i.ToString()+"", typeof(decimal),"(BIN"+i.ToString()+" /
[In System])*100");
if (i == 1)
{
dt.Columns.Add("100-%BIN1", typeof(double), "100 -[%BIN1]");
Avg[i] = Convert.ToDouble(dt.Compute("Avg([col"+i.Tostring()+"])", string.Empty));
}
else
{
Avg[i] = Convert.ToDouble(dt.Compute("Avg([col" + i.ToString() + "])", string.Empty));
}
dt.Rows.Add("Avg_xbar"+i.ToString()+"", Avg[i]);
}
From my code above,There is a problem about result is below:
col1 | col2 | col3 | ... | col20 |
-----------|------|------|-----|-------|
5 | 10 | 5 | ... | 8 |
-----------|------|------|-----|-------|
8 | 7 | 8 | ... | 2 |
-----------|------|------|-----|-------|
Avg_xbar1 | 6.5 | | | |
-----------|------|------|-----|-------|
Avg_xbar2 | 8.5 | | | |
-----------|------|------|-----|-------|
Avg_xbar3 | 6.5 | | | |
-----------|------|------|-----|-------|
... | ... | | | |
-----------|------|------|-----|-------|
Avg_xbar20 | 5 | | | |
I'm assuming that you've got that code running in a loop for each column. You'll want to move the addition of rows outside of this loop.
for (var i = 0; i < columns.length; i++) {
// existing looping logic
}
dt.Rows.Add(Avg);
my table
+----+----------+---------+---------+----------+
| ID | State A | State B | State C | Duration |
+----+----------+---------+---------+----------+
| 1 | a | a | a | 12 |
+----+----------+---------+---------+----------+
| 2 | a | a | a | 15 |
+----+----------+---------+---------+----------+
| 3 | a | a | b | 12 |
+----+----------+---------+---------+----------+
| 4 | a | b | b | 30 |
+----+----------+---------+---------+----------+
| 5 | b | b | b | 12 |
+----+----------+---------+---------+----------+
IF THE 3 COLUMNS AS THE SAME VALUE AND DURATION <15
result as the following :
+----+-----------------+
| ID | THE RESULT |
+----+-----------------+
| 1 | a is the winner |
+----+-----------------+
This is a filter along with concat():
select concat(statea, ' is the winner')
from t
where statea = stateb and stateb = statec and duration < 15
I am working on a small project using C# and EF5.0 and I need to group some data. Let say I have table of columns in a building like shown below.
+----------+--------Columns Table--+------+------+
| ColumnID |ColumnName|Width|Length|Height|number|
+----------+----------+-----+------+------+------+
| 1 | C101 | 50 | 70 | 250 | 1 |
| 2 | C102 | 70 | 70 | 250 | 1 |
| 3 | C103 | 70 | 60 | 250 | 1 |
| 4 | C104 | 90 | 70 | 250 | 1 |
| 5 | C105 | 40 | 50 | 250 | 1 |
| 6 | C106 | 50 | 70 | 250 | 1 |
| 7 | C107 | 50 | 60 | 250 | 1 |
| 8 | C108 | 70 | 70 | 250 | 1 |
+----------+----------+-----+------+------+------+
I need a C# code to see the above data groupped like this:
+----------+---Groupped Columns Table-----+------+
|G_ColumnID|ColumnName|Width|Length|Height|number|
+----------+----------+-----+------+------+------+
| 1 |C(101-106)| 50 | 70 | 250 | 2 |
| 2 |C(102-108)| 70 | 70 | 250 | 2 |
| 3 | C103 | 70 | 60 | 250 | 1 |
| 4 | C104 | 90 | 70 | 250 | 1 |
| 5 | C105 | 40 | 50 | 250 | 1 |
| 6 | C107 | 50 | 60 | 250 | 1 |
+----------+----------+-----+------+------+------+
I prefer clues than the exact solution.
EDIT : Below code shows my current state. I think I can find the columns with the same Height, Width and Length using this code. But I am not sure how to generate a new name for the group.
using (pehlivanEntities context = new pehlivanEntities())
{
foreach (var item in context.table1)
{
int id = item.ColumnID;
foreach (var item2 in context.table1)
{
int id2 = item2.ColumnID;
if (id != id2)
{
if (item.Width == item2.Width)
{
if (item.Length == item2.Length)
{
if (item.Height == item2.Height)
{
//Alter item.ColumnName
//increase item.number by one
//Remove item2
}
}
}
}
}
}
}
Well you'd start with grouping on a composite key:
var groups = myData.GroupBy(d => new{d.Width, d.Length, d.Height})
then
groups
.Select(g => new {
g.Key.Width,
g.Key.Length,
g.Key.Height,
columnNames = g.Select(x => x.ColumnName),
number = g.Count()})
then a bit of string manipulation on the columnNames field
I have 2 DataGridViews (DGVs).
theChipDGV will contain data like this (except with many more columns):
______________________________________________________________
| NAME | P/N | X | Y | ROTATION | PACKAGE |
|________|________|________|________|____________|___________|
| R16 | 147479 | 20.325 | 100.000| 0 | 0603 |
| C6 | 14739 | -5.325 | -10.105| 180 | 0603 |
| U45 | 123456 | 12.345 | 12.345 | 45 | 0402 |
|________|________|________|________|____________|___________|
theDataBaseDGV will contain data like this (except with many more columns):
____________________________________________________________________________________________
| PACKAGE | DESCRIPTION | FEEDER | VISION | SPEED | MACHINE | WIDTH | TIME |
|___________|_______________|__________|__________|_________|___________|_________|_______ |
| PLCC20 | N/A | 25MM | N/A | 3 | UNIVERSAL | 12MM | 0.05 |
| 0603 | 0603C_1.0 | 8X4 | 1 | 1 | FUJI-1 | 8MM | 20 |
| 0603 | 0603R_1.0 | 12X4 | 1 | 5 | FUJI-2 | 16MM | 0.20 |
|___________|_______________|__________|__________|_________|___________|_________|_______ |
What I would like to do is match the column in theChipDGV labeled PACKAGE with the same labeled column in theDataBaseDGV. If there is a match, the entire row will be concatted into a new DGV (let's label it: theFinalDGV). Also, if the PACKAGE type is matched and is also in the next line (like 0603) it will check to see if the column labeled Name in theChipDGV starts with a R or a C. Depending on which it starts with will determine the rest of the columns from theDataBaseDGV that will be used.
SO:
theFinalDGV will look like this:
_____________________________________________________________________________________________________________________________________________
| NAME | P/N | X | Y | ROTATION | PACKAGE | DESCRIPTION | FEEDER | VISION | SPEED | MACHINE | WIDTH | TIME |
|________|________|________|________|____________|___________|_______________|__________|__________|_________|___________|_________|________|
| R16 | 147479 | 20.325 | 100.000| 0 | 0603 | 0603R_1.0 | 12X4 | 1 | 5 | FUJI-2 | 16MM | 0.20 |
| C6 | 14739 | -5.325 | -10.105| 180 | 0603 | 0603C_1.0 | 8X4 | 1 | 1 | FUJI-1 | 8MM | 20 |
| U45 | 123456 | 12.345 | 12.345 | 45 | 0402 | | | | | | | |
|________|________|________|________|____________|___________|_______________|__________|__________|_________|___________|_________|________|
Notice, if there is no match it leaves the columns empty.
So:
Does anyone know how I can possibly go about doing this? I mostly would like to know how to match the values from 1 column with another and if there are multiple columns from theDataBaseDGV that have the same values.. then how to properly match those.
int chipRowCount = theChipDGV.RowCount;
int dataBaseRowCount = theDataBaseDGV.RowCount;
for (int count = 0; count < chipRowCount; count++)
{
for (int i = 0; i < dataBaseRowCount; i++)
{
if (theChipList[count].PkgStyle.Equals(theDataBaseList[i].PackageType) || (theChipList[count].PkgStyle.Contains(theDataBaseList[i].PackageType) &&
theDataBaseList[i].PartDescription.Contains("R") && theChipList[count].Name.StartsWith("R")) || ((theChipList[count].PkgStyle.Contains(theDataBaseList[i].PackageType) &&
theDataBaseList[i].PartDescription.Contains("C") && theChipList[count].Name.StartsWith("C"))))
{
if (!theChipList[count].Used)
{
theChipList[count].Used = true;
theFinalList.Add(new LoadLine(theChipList[count].Name, theChipList[count].PartNumber,
theChipList[count].XPlacement, theChipList[count].YPlacement, theChipList[count].Rotation,
theChipList[count].PkgStyle, theDataBaseList[i].PackageType, theDataBaseList[i].PartDescription,
theDataBaseList[i].Feeder, theDataBaseList[i].Vision, theDataBaseList[i].Speed,
theDataBaseList[i].Machine, theDataBaseList[i].TapeWidth, 0));
theFinalDGV.DataSource = theFinalList;
}
}
}
}
for (int count = 0; count < theChipList.Count; count++)
{
if (!theChipList[count].Used)
{
theFinalList.Add(new LoadLine(theChipList[count].Name, theChipList[count].PartNumber,
theChipList[count].XPlacement, theChipList[count].YPlacement, theChipList[count].Rotation,
theChipList[count].PkgStyle, string.Empty, string.Empty, string.Empty, string.Empty,
string.Empty, string.Empty, string.Empty, 0));
}
}