Post: [C#] Random Strings
06-13-2011, 10:19 PM #1
Correy
I'm the Original
(adsbygoogle = window.adsbygoogle || []).push({}); in this thread i will be showing you how to create and code Random commands using Strings in Visual Studio 2011 C#


Using strings which are already set
Add this code to you're option you want to add it to, in this case i will be adding it to a public void to create some randomise Label text..
    
{
String[] strLabel = {
"First Label",
"Second Label",
"Third Label",
"Fourth Label"
};
Random r = new Random();
int iSelect = r.Next(0, 3);
lblRandom.Text = strLabel[iSelect];
}

now, if you were going to add strings make sure you change the 'r.Next(COLOR="red"]0,3[/COLOR])' as this shows how many strings you want to generate.


you can use this to also do commands, for example..
    
button1 = new Button
{
String[] button = {
command one();,
command two();,
command three();,
command four();"
};
Random r = new Random();
int iSelect = r.Next(0, 3);
button1_click = button[iSelect];
}


Video Example Here.



it's a simple code which i came up with in a test form, it may not be the best but it certainly works and Can come in use.
thanks for reading.
Last edited by Correy ; 06-15-2011 at 01:04 AM.
06-14-2011, 04:09 PM #2
kiwimoosical
Bounty hunter
    public string GenRandomString(int length, char[] charset)
{
List<char> str = new List<char>();
for (int x = 0; x < length; x++)
str.Add(charset[new Random().Next(charset.Length - 1)]);
return new String(str.ToArray());
}

Supply it the length of the string you want and the characters you want in the string and it will return a psuedo-randomized string, computers can't produce a truly random string..
06-14-2011, 08:47 PM #3
Correy
I'm the Original
Originally posted by kiwimoosical View Post
    public string GenRandomString(int length, char[] charset)
{
List<char> str = new List<char>();
for (int x = 0; x < length; x++)
str.Add(charset[new Random().Next(charset.Length - 1)]);
return new String(str.ToArray());
}

Supply it the length of the string you want and the characters you want in the string and it will return a psuedo-randomized string, computers can't produce a truly random string..


nice coding, you make this yourself ?
06-14-2011, 08:58 PM #4
kiwimoosical
Bounty hunter
Originally posted by xCorrey
nice coding, you make this yourself ?


I make everything myself.. So yes I did..
06-14-2011, 09:10 PM #5
Epic?
Awe-Inspiring
You can't really randomize anything on a computer. I mean, what appears to be random, is actually generated from a complex code/algorithm. Its hard to crack, but certainly not random.
06-14-2011, 11:03 PM #6
kiwimoosical
Bounty hunter
Originally posted by AsianInvasion View Post
You can't really randomize anything on a computer. I mean, what appears to be random, is actually generated from a complex code/algorithm. Its hard to crack, but certainly not random.


The algorithm shits bricks on real random ( sarcasm obv... :P )
You must login or register to view this content.
Feel free to browse
06-15-2011, 01:01 AM #7
Correy
I'm the Original
Originally posted by kiwimoosical View Post
I make everything myself.. So yes I did..


nice bro, mine was easier and more understanding but yours functions better
Last edited by Correy ; 06-15-2011 at 01:03 AM.
06-15-2011, 04:44 PM #8
kiwimoosical
Bounty hunter
Originally posted by xCorrey
nice bro, mine was easier and more understanding but yours functions better


Mine is easier too:
    labelWhatever.Text = GenRandomString(10, "abcdefghijklmnopqrstuvwxyz1234567890".ToCharArray());
Last edited by kiwimoosical ; 06-16-2011 at 04:30 PM.
06-15-2011, 10:39 PM #9
Correy
I'm the Original
Originally posted by kiwimoosical View Post
Mine is easier too:
    labelWhatever.Text = GenRandomString(10, "abcdefghijklmnopqrstuvwxyz1234567890".ToCharArray());[/QUOTE]

thanks, but that was the random label text, i was trying to make it generate randomly the fixed label..
06-16-2011, 04:30 PM #10
kiwimoosical
Bounty hunter
Originally posted by xCorrey
thanks, but that was the random label text, i was trying to make it generate randomly the fixed label..


Then why did you title this random strings?

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo