|
None of that matters as long as you keep making new instances of Random -- do not instantiate Random inside the for loop!
|
|
|
|
|
OK, can you provide the declaration and the content (just before entering the for loop) of zeroFrequencyLetterList? If it varies, just a typical case; if complex, a simplified one that still makes you unhappy about Random.Next
|
|
|
|
|
Off topic, so feel free to ignore this, but your loop count looks a bit odd to me. You increment i and test for it being less than zeroFrequencyLetterList.Count. But you remove an entry from the list each time through the loop. So, this loop will drop out before it has processed the entire list. Here is what happens if you have 10 entries in the list:
i Count
=== =====
0 10
1 9
2 8
3 7
4 6
5 5
The loop will stop at this point, so although you have 10 entries in the list you will only process 5 of them.
Not only that, but you will skip entries in the list. First time through, i is 0 so you remove the entry at index 0, so the entry that was at index 1 is now at index 0, and the entry at index 2 is now at 1. Next time through, i is 1 so you now pick up the entry at index 1 which means you have skipped the entry which was previously at index 1 but is now at index 0. (That's a bit confused, but if you think about it you'll see what I mean.)
That may be what you want, but it looks a bit strange to me.
|
|
|
|
|
you absolutely right,
but luckily i had there another condition && decrypted < 25 therefor it stopped the loop just on time
but you right, I fixed it
Random r = new Random();
int leftLetters = zeroFrequencyLetterList.Count;
for (int i = 0; i < leftLetters && decrypted < 25; i++)
{
bool flag = true;
int randomed;
do
{
randomed = r.Next(0, zeroFrequencyLetterList.Count);
if (encryptionKey[zeroFrequencyLetterList[randomed]] != ' ')
flag = false;
else
flag = true;
} while (!flag);
encryptionKey[zeroFrequencyLetterList[0]] = abcLetters[randomed];
abcLetters.RemoveAt(randomed);
zeroFrequencyLetterList.RemoveAt(0);
decrypted++;
}
modified on Friday, April 9, 2010 8:50 AM
|
|
|
|
|
I still don't think this works because you are removing an entry from the list each time through. Here is what happens:
i = 0; list = 0, 1, 2, 3; list[i] = 0; remove i
i = 1; list = 1, 2, 3; list[i] = 2; remove i
i = 2; list = 1, 3; list[i] = oops!
|
|
|
|
|
i'm not removing at i's place, i'm removing the first place of the collection
|
|
|
|
|
I ran some tests with this code:
public override void Test(int arg) {
int n=10;
int max=1000000;
if (arg==2) { n=10; max=1000; }
if (arg==3) { n=3; max=100; }
Random r=new Random();
int[,] hist=new int[n,n];
int prev=0;
for (int i=0; i< max; i++) {
int j=r.Next(0, n);
hist[prev, j]++;
prev=j;
}
for (prev=0; prev< n; prev++) {
string s="";
for (int j=0; j< n; j++) {
s+=hist[prev, j].ToString().PadLeft(8);
}
log(s);
}
}
and don't see anything wrong with Random, not even over a small [min,max) range.
|
|
|
|
|
Not sure what your problem is - it works as expected for me and as Luc says (and he's not often wrong - if ever. )
Here's a little console app that does what I think you're doing.
Note the bogus variables - you can adjust them to see what happens.
(Gets rid of unnecessary flag but logic is the same. )
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Tester();
Console.ReadLine();
}
public static void Tester()
{
int[] zeroFrequencyLetterList = { 1, 2, 3, 4, 0, 5, 10, 9, 8, 7, 6, 5, 10, 2, 9, 7, 0, 6 };
int decrypted = 0;
char[] encryptionKey = { 'a', 'b', ' ', 'c', 'd', 'e', ' ', ' ', 'f', ' ', 'g' };
Random r = new Random();
for (int i = 0; i < zeroFrequencyLetterList.Count() && decrypted < 25; i++)
{
int randomed;
char aLetter;
do
{
randomed = r.Next(0, zeroFrequencyLetterList.Count());
aLetter = encryptionKey[zeroFrequencyLetterList[randomed]];
Console.WriteLine("{0,3}: {1,3} -> {2}", i, randomed, aLetter );
} while (aLetter == ' ');
decrypted++;
}
}
}
}
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
You create r outside the loop and reuse it. The OP created a new Random instance every time. It is possible that Random has some sort of bias to the first number which it generates, depending on the internal algorithm it uses. It certainly seemed that way when I tried it (completely unscientific, I admit).
|
|
|
|
|
I am pretty sure the seed for the random number is being intialized the same or close enough to skew the results thanks to being in that loop.
Random is actually fairly predictable if you know the seed and the range.
It's how they make those wonderful authenticator programs and keyfabs.
Synch it with one on another site and both will have the exact same number every time.
Check out the seeds comments[^]
|
|
|
|
|
|
In my application i have a picture box and scalablepicturebox control,i need to set image from picturebox to my scalablepictureboxcontrol which is present in form2 and not on mainform but my picturebox is on main form.so what to do ?? so please let me know the piece of code for it.
|
|
|
|
|
Either this is related to
A scrollable, zoomable, and scalable picture box[^] in which case use the "New Message" button at the bottom of the article as it causes the author to receive an email telling him you have a problem.
Or this is "I need to copy information from one form to another and don't know how." In which case see
http://www.codeproject.com/tips/55555/Pass-value-between-forms-using-events.aspx[^]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
hi i want to filter base on lastname and firstname encode by the user in textbox1 and texbox2.. but i'm having hard time for the right code for this..heres my code but not functioning.. plz help hows the right way..
<br />
<br />
<br />
bindingSource1.Filter = "LASTNAME like '" + textBox1.Text.Trim() + "%'" AND FIRSTNAME like '" + textBox2.Text.Trim() + "%'";<br />
<br />
bindingSource1.Sort = "LASTNAME";<br />
<br />
<br />
<br />
<br />
|
|
|
|
|
What is happening? Or not happening that you expect to happen?
Obviously, it will depend on the content of textBox1 and textBox2, but I imaging they are pretty simple and you have checked them.
Try replacing the textboxes with something you know will match in the DB:
"LASTNAME like 'JON%' AND FIRSTNAME like 'MAT%'"
with a MATTHEW JONES in the DB.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
i want to know to retrieve mails using the pop3 from gmail
|
|
|
|
|
Is Google not working where you are? A simple search using your own words with "C#" tagged on teh end gives thousands of code examples! "retrieve mails using the pop3 from gmail c#" is what I tried...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
|
hi guys,
Iam try to validate a south african phone number.
I have tried this but it won't work:
Regex rx = new Regex(((?:\\+27|27)|0)(72|82|73|83|74|84)(\\d{7}));
here is an example of numbers I would want to validate:
- 0 74 3720198
- 0 72 3720198
- 27 74 3720198
- +27 74 3720198
Please help me if Iam making a mistake somewhere
I have tried google but no help so far...
Thanks,
Morgan Freeman
|
|
|
|
|
I'm afraid I can't help much with the regex for SA numbers, as I don't know the ins-and-outs of how they are comprised. (Someone else here might be able to help directly), but this [free] tool will speed up validation:
http://www.radsoftware.com.au/regexdesigner/[^]
Also this could be of some help:
http://regexlib.com/Search.aspx?k=south+africa[^]
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
thanks man
I will try your links and thanks alot
Morg
|
|
|
|
|
Your first problem is the spaces between the digits.
Try something a bit simpler, and work your way up to the full expression:
"((0\s)|(27\s))(?<AreaCode>[0-9]{2})(?<LocalNumber>\s[0-9]{7})"
Feed this into Expresso[^] - examines and generates Regular expressions. It's free, and it really helps!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
OriginalGriff wrote: Your first problem is the spaces between the digits
thanks man, with spaces I was just trying to indicate where the full number is branched or divided
I will try your idea and thanks alot
Morg
|
|
|
|
|
hey hey thanks man that program is so cool, I managed
to figure out what was wrong, the right expr:
Regex rx = new Regex("((?:\+27|27)|0)(72|82|73|83|74|84)(\d{7})");
Great one!
|
|
|
|
|
I wish I'd written it!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|