Array only printing one number - asp.net

I am having trouble printing my array in a asp:Label. the array is 5 numbers long, but it will only print out one of the numbers when I do the id.Text = arrayname;
Here is the method:
void random4helper()
{
Random rand = new Random();
int min = 1;
int max = 51;
int randomNum;
int i = 0;
int count = 0;
bool loop = true;
while (loop)
{
randomNum = rand.Next(min, max);
if (!meganumbers4.Contains(randomNum))
{
meganumbers4[i] = randomNum;
count += 1;
i += 1;
}
if (count == 5)
{
loop = false;
}
}
for (int j = 0; j < meganumbers4.Length; j++)
{
d.Text = meganumbers4[j] + " ";
}
}
Here is the corresponding Label:
<asp:Label runat="server" id="a" CssClass="print" Text="A"></asp:Label>

You are overwriting the value in each loop. Instead, you need to add the new value to the current value. Change this line:
d.Text = meganumbers4[j] + " ";
To:
d.Text += meganumbers4[j] + " ";

Related

How to solve this Range Error in my code?

I'm trying to create an cipher app on android studio using flutter. Right now I'm working on a simple Atbash Cipher, but I get a range error when trying to test it. These are the encrypt and decrypt codes:
#override
String encrypt(String plaintext, {String key}) {
String alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alfaReverso = "";
for(int i = alfa.length-1; i > -1; i++){
alfaReverso += alfa[i];
}
String encryText = "";
for (int i = 0; i < plaintext.length; i++){
if(plaintext.codeUnitAt(i) == 32){
encryText += " ";
}
else{
int count = 0;
for(int j = 0; j < alfa.length; j++){
if(plaintext[i] == alfa[j]){
encryText += alfaReverso[j];
break;
}
}
}
}
return "ENCRYPT Plain = " + encryText;
}
}
#override
String decrypt(String cyphertext, {String key}) {
String alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alfaReverso = "";
for(int i = alfa.length-1; i > -1; i++){
alfaReverso += alfa[i];
}
String dencryText = "";
for (int i = 0; i < cyphertext.length; i++){
if(cyphertext.codeUnitAt(i) == 32){
dencryText += " ";
}
else{
int count = 0;
for(int j = 0; j < alfaReverso.length; j++){
if(cyphertext[i] == alfaReverso[j]){
dencryText += alfa[j];
break;
}
}
}
}
return "ENCRYPT Plain = " + dencryText;
}
When trying to run it this is the range exception I get:
I/flutter ( 6004): RangeError (index): Invalid value: Not in range 0..25, inclusive: 26
I know it has something to do with the alphabetI'm using, but I don't know how to solve it.
There is an error when you start from the highest index:
for(int i = alfa.length-1
Your index has to go down and you are using ++.
Use this:
for(int i = alfa.length-1; i > -1; i--)

Make a Constraint to sorting an Array (Choco + java )

I am trying to develop a choco solver to the probelm of the Planning of telephone support center. in 12 hours from 8:00 o clock to 20:00.
variables and constraint :
Number of employees = 9
Minimum ans Maximum buisiness hours for every employee (h and H)
buisiness hours foe all employees : 42 hours <= total hours <= 42+C (C in my case equals 2)
Table of numbers of employee who work in every hour ( size of table =12 )
Contrainst who i cant'make :
I got to know the number of nuisiness hours for each employee but I can not put them in tracking hours :/
the result will be :
Final Result
but until now i got my result untill now
I think it's sort problem... please if you can just save my life and tell me what is the missing constraint in my code.
My code
package projetppc;
import java.util.Arrays;
import javax.swing.SortingFocusTraversalPolicy;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.variables.IntVar;
public class ProjetPPC {
public void modelAndSolve() {
int k = 9;
int htpj = 12;
int h = 4;
int H = 6;
int C = 2;
int HT = 42;
Model model = new Model(k + "- CAT");
int[] numOfemp = {1, 2, 4, 5, 5, 4, 5, 5, 3, 4, 2, 2};
IntVar[][] matrix = new IntVar[k][htpj];
for (int i = 0; i < k; i++) {
for (int j = 0; j < htpj; j++) {
matrix[i][j] = model.intVar("(" + i + "," + j + ")", 0, 1);
}
}
model.arithm(matrix[0][0], "=", 1).post();
int[] coeffs1 = new int[htpj];
Arrays.fill(coeffs1, 1);
// constraint 1 et 2
for (int i = 0; i < k; i++) {
model.scalar(matrix[i], coeffs1, "<=", H).post();
model.scalar(matrix[i], coeffs1, ">=", h).post();
}
int[] coeffs2 = new int[k];
Arrays.fill(coeffs2, 1);
IntVar[][] inversematrix = new IntVar[htpj][k];
for (int i = 0; i < k; i++) {
for (int j = 0; j < htpj; j++) {
inversematrix[j][i] = matrix[i][j];
}
}
// constraint
for (int i = 0; i < htpj; i++) {
model.scalar(inversematrix[i], coeffs2, "=", numOfemp[i]).post();
}
// constraint
IntVar[] alltable = new IntVar[k * htpj];
for (int i = 0; i < k; i++) {
for (int j = 0; j < htpj; j++) {
alltable[(htpj * i) + j] = matrix[i][j];
}
}
int[] coeffs3 = new int[k * htpj];
Arrays.fill(coeffs3, 1);
model.scalar(alltable, coeffs3, ">=", HT).post();
model.scalar(alltable, coeffs3, "<=", HT + C).post();
// solution
Solution solution = model.getSolver().findSolution();
if (solution != null) {
for (int i = 0; i < k; i++) {
System.out.println("employé " + i + " " + Arrays.toString(matrix[i]));
}
} else {
System.out.println("Pas de solution.");
}
}
public static void main(String[] args) {
new ProjetPPC().modelAndSolve();
}
}

Alternative to System.Web.Security.Membership.GeneratePassword in aspnetcore (netcoreapp1.0)

Is there any alternative to System.Web.Security.Membership.GeneratePassword in AspNetCore (netcoreapp1.0).
The easiest way would be to just use a Guid.NewGuid().ToString("n") which is long enough to be worthy of a password but it's not fully random.
Here's a class/method, based on the source of Membership.GeneratePassword of that works on .NET Core:
public static class Password
{
private static readonly char[] Punctuations = "!##$%^&*()_-+=[{]};:>|./?".ToCharArray();
public static string Generate(int length, int numberOfNonAlphanumericCharacters)
{
if (length < 1 || length > 128)
{
throw new ArgumentException(nameof(length));
}
if (numberOfNonAlphanumericCharacters > length || numberOfNonAlphanumericCharacters < 0)
{
throw new ArgumentException(nameof(numberOfNonAlphanumericCharacters));
}
using (var rng = RandomNumberGenerator.Create())
{
var byteBuffer = new byte[length];
rng.GetBytes(byteBuffer);
var count = 0;
var characterBuffer = new char[length];
for (var iter = 0; iter < length; iter++)
{
var i = byteBuffer[iter] % 87;
if (i < 10)
{
characterBuffer[iter] = (char)('0' + i);
}
else if (i < 36)
{
characterBuffer[iter] = (char)('A' + i - 10);
}
else if (i < 62)
{
characterBuffer[iter] = (char)('a' + i - 36);
}
else
{
characterBuffer[iter] = Punctuations[i - 62];
count++;
}
}
if (count >= numberOfNonAlphanumericCharacters)
{
return new string(characterBuffer);
}
int j;
var rand = new Random();
for (j = 0; j < numberOfNonAlphanumericCharacters - count; j++)
{
int k;
do
{
k = rand.Next(0, length);
}
while (!char.IsLetterOrDigit(characterBuffer[k]));
characterBuffer[k] = Punctuations[rand.Next(0, Punctuations.Length)];
}
return new string(characterBuffer);
}
}
}
I've omitted the do...while loop over the CrossSiteScriptingValidation.IsDangerousString. You can add that back in yourself if you need it.
You use it like this:
var password = Password.Generate(32, 12);
Also, make sure you reference System.Security.Cryptography.Algorithms.
System.Random doesn't provide enough entropy when used for security reasons.
https://cwe.mitre.org/data/definitions/331.html
Why use the C# class System.Random at all instead of System.Security.Cryptography.RandomNumberGenerator?
Please see the example below for a more secure version of #khellang version
public static class Password
{
private static readonly char[] Punctuations = "!##$%^&*()_-+[{]}:>|/?".ToCharArray();
public static string Generate(int length, int numberOfNonAlphanumericCharacters)
{
if (length < 1 || length > 128)
{
throw new ArgumentException("length");
}
if (numberOfNonAlphanumericCharacters > length || numberOfNonAlphanumericCharacters < 0)
{
throw new ArgumentException("numberOfNonAlphanumericCharacters");
}
using (var rng = RandomNumberGenerator.Create())
{
var byteBuffer = new byte[length];
rng.GetBytes(byteBuffer);
var count = 0;
var characterBuffer = new char[length];
for (var iter = 0; iter < length; iter++)
{
var i = byteBuffer[iter] % 87;
if (i < 10)
{
characterBuffer[iter] = (char)('0' + i);
}
else if (i < 36)
{
characterBuffer[iter] = (char)('A' + i - 10);
}
else if (i < 62)
{
characterBuffer[iter] = (char)('a' + i - 36);
}
else
{
characterBuffer[iter] = Punctuations[GetRandomInt(rng, Punctuations.Length)];
count++;
}
}
if (count >= numberOfNonAlphanumericCharacters)
{
return new string(characterBuffer);
}
int j;
for (j = 0; j < numberOfNonAlphanumericCharacters - count; j++)
{
int k;
do
{
k = GetRandomInt(rng, length);
}
while (!char.IsLetterOrDigit(characterBuffer[k]));
characterBuffer[k] = Punctuations[GetRandomInt(rng, Punctuations.Length)];
}
return new string(characterBuffer);
}
}
private static int GetRandomInt(RandomNumberGenerator randomGenerator)
{
var buffer = new byte[4];
randomGenerator.GetBytes(buffer);
return BitConverter.ToInt32(buffer);
}
private static int GetRandomInt(RandomNumberGenerator randomGenerator, int maxInput)
{
return Math.Abs(GetRandomInt(randomGenerator) % maxInput);
}
}

Create 2 or more text files with ASP.NET

I have created a web app which creates 1 text file. Inside this text file it is created 1000 rows with the same word "TRY AGAIN". After this each 50 rows I put a random code which means in 1000 rows, 20 rows are random.
This is my code:
static Random randNum = new Random();
public static string Random(int ran)
{
string _charachters = "ABCDEFGHIJKMLNOPQRSTUVWXYZ0123456789";
char[] chars = new char[ran];
int allowedCharCount = _charachters.Length;
for (int i = 0; i < ran; i++)
{
chars[i] = _charachters[(int)((_charachters.Length) * randNum.NextDouble())];
}
return new string(chars);
}
protected void Button1_Click(object sender, EventArgs e)
{
string pathCreate = #"C:\" + TextBox3.Text + ".txt";
if (!File.Exists(pathCreate))
{
using (StreamWriter sw = File.CreateText(pathCreate))
{
for (int i = 1; i <= int.Parse(TextBox1.Text); i++)
{
sw.WriteLine("TRY AGAIN.");
}
}
}
string pathRandom = #"C:\" + TextBox3.Text + ".txt";
string[] lines = File.ReadAllLines(pathRandom);
for (int i = 0; i < lines.Length; i += int.Parse(TextBox2.Text))
{
lines[i] = lines[i].Replace("TRY AGAIN.", Random(int.Parse("7")));
}
File.WriteAllLines(pathRandom, lines);
}
Now I want to create 2 ore more text files with one click of a button. And on each text file there will be random codes (not duplicates). Any idea?
Thank You.
I found the solution. It is late in my country and my brain barely works. :P
for(int j = 1; j <= 10; j++)
{
string pathKrijo = #"C:\inetpub\wwwroot\KODET\" + j.ToString() + ".txt";
using (StreamWriter sw = File.CreateText(pathKrijo))
{
for (int i = 1; i <= 100; i++)
{
sw.WriteLine("Provo Përsëri.");
}
}
string pathKodFitues = #"C:\inetpub\wwwroot\KODET\" + j.ToString() + ".txt";
string[] lines = File.ReadAllLines(pathKodFitues);
for (int i = 0; i < lines.Length; i += 10)
{
lines[i] = lines[i].Replace("Provo Përsëri.", Random(int.Parse("7")));
}
File.WriteAllLines(pathKodFitues, lines);
}

How can I partition a QByteArray efficiently?

I want to partition a QByteArray message efficiently, so this function I implemented take the Bytes, the part I want to extract, and toEnd flag which tells if I want to extract part1 till the end of the array. my dilimeter is spcae ' '
example if I have:
ba = "HELLO HOW ARE YOU?"
ba1 = getPart(ba, 1, false) -> ba1 = "HELLO"
ba2 = getPart(ba, 2, true) -> ba2 = "HOW ARE YOU?"
ba3 = getPart(ba, 3, false) -> ba3 = "ARE"
the function below works just fine, but I am wondering if this is efficient. should I consider using split function?
QByteArray Server::getPart(const QByteArray message, int part, bool toEnd)
{
QByteArray string;
int startsFrom = 0;
int endsAt = 0;
int count = 0;
for(int i = 0; i < message.size(); i++)
{
if(message.at(i) == ' ')
{
count++;
if(part == count)
{
endsAt = i;
break;
}
string.clear();
startsFrom = i + 1;
}
string.append(message.at(i));
}
if(toEnd)
{
for(int i = endsAt; i < message.size(); i++)
{
string.append(message.at(i));
}
}
return string;
}
What about this:
QByteArray Server::getPart(const QByteArray& message, int part, bool toEnd)
{
int characters(toEnd ? -1 : message.indexOf(' ', part) - part);
return message.mid(part, characters);
}
Why not make it a regular QString and use split. That will give you a QStringList.

Resources