• Generating Random Strings
    9 replies, posted
How would you do it? I'm doing it like this [code] private void button1_Click(object sender, EventArgs e) { int i = 50; Random rnd = new Random(); while (i-- > 0) txtKey.Text = GenerateKey(rnd, 32); } public string GenerateKey(Random rnd, int Length) { string charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!£$%^&*"; StringBuilder sb = new StringBuilder(); while (Length-- > 0) sb.Append(charPool[(int)rnd.NextDouble() * charPool.Length]); return sb.ToString(); } [/code]This just returns AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Any ideas why? Would you do it a different way?
How about getting a random number from 0 to 25, add 40 and then get the char from that ASCII? Shouldn't be hard to make. (btw, that only generates upper case letters)
Could use array... I suppose...
Well, this works. It's C# btw. [cpp]using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Random random=new Random(); int i = 0; while (i < 10) { int randNum; if (random.NextDouble() > 0.5) { randNum = (int)(random.NextDouble() * 25) + 65; } else { randNum = (int)(random.NextDouble() * 25) + 97; } Console.Write((char)randNum); ++i; } Console.ReadKey(); } } } [/cpp] Generates a string of 10 chars which are random upper and lower case letters of the English alphabet.
(int)rnd.NextDouble() * charPool.Length should be (int)(rnd.NextDouble() * charPool.Length) else the double is casted into an int and then multiplied by the length, with a chance of 2^24-1 to 1 resulting in 0. Edit: The probability is wrong, since double has a bunch of NaNs, INFs 'n stuff, but it's still very probably to result in 0 :P
-snip- Wow, very stupid of me. I didn't realize you commented to the OP.
[QUOTE=ZeekyHBomb;21310303](int)rnd.NextDouble() * charPool.Length should be (int)(rnd.NextDouble() * charPool.Length) else the double is casted into an int and then multiplied by the length, with a chance of 2^24-1 to 1 resulting in 0. Edit: The probability is wrong, since double has a bunch of NaNs, INFs 'n stuff, but it's still very probably to result in 0 :P[/QUOTE] Shit, yeah. Damn I'm getting worse at this stuff. Spotting errors was never my thing :rolleyes:
[QUOTE=Loli;21310873]Shit, yeah. Damn I'm getting worse at this stuff. Spotting errors was never my thing :rolleyes:[/QUOTE] That should come in due time. Once you've struggled enough with certain errors you should be able to catch and fix them whilst half asleep.
[cpp] private static RandomNumberGenerator rng; static string GetRandomString(char[] chars,int length) { if (length < 1) return ""; StringBuilder stringBuilder = new StringBuilder(length); if (rng == null) rng = RandomNumberGenerator.Create(); byte[] bytes = new byte[length]; rng.GetBytes(bytes); for (int i = 0; i < length; i++) stringBuilder.Append(chars[bytes[i]%chars.Length]); return stringBuilder.ToString(); }[/cpp] Needs System.Security.Cryptography and System.Text [editline]12:13AM[/editline] No, but seriously, why are so many people saying to use doubles? Really? [editline]12:14AM[/editline] Oh, and one limitation, chars needs to be under 256 chars. Otherwise, it'll only use the first 256. But that should be easy to fix.
[QUOTE=Kirth;21312160]That should come in due time. Once you've struggled enough with certain errors you should be able to catch and fix them whilst half asleep.[/QUOTE] I'm looking at new areas of programming at the moment, so I suppose these new errors are just burrowing into my head slowly...
Sorry, you need to Log In to post a reply to this thread.