user1963864
user1963864

Reputation: 69

random number generator to be displayed on a text box

I'm kind of stuck on a thing I'm working on.. I have a windows form application with different buttons, and each button is supposed to do different thing. Now my problem:

On one button, when I press it, it should generate random numbers (0-1000 for example) and display the number in a textbox, which I also have on the program. I tried to do this code on the button:

private void button5_Click(object sender, EventArgs e)
{
    Random slumpGenerator = new Random(); int tal; 
    tal = slumpGenerator.Next(); 
}

But unfortunately, no number is displayed on the text box. And i think it could be because I haven't referred that the numbers should display on my textbox, any ideas?

Upvotes: 2

Views: 22223

Answers (5)

Thangamalai
Thangamalai

Reputation: 11

                       **Random Number Generation in C#.Net**

Add two namespaces before you write the code

*using System.Security;

using System.Security.Cryptography;*

Code:

Copy and place the following code inside the button

RNGCryptoServiceProvider xx = new RNGCryptoServiceProvider();
byte [] random_number=new  byte [512];
xx.GetBytes(random_number);
foreach (var i in random_number)
{
    textBox1.Text = i.ToString();
}

for further details in c#.net refer my blogspot : mbthangamalai.blogspot.in

Upvotes: 1

Dozer789
Dozer789

Reputation: 2036

You need to add a few things to your code. Here is the fulll code for what you want...

private void button5_Click(object sender, EventArgs e)
{
    Random slumpGenerator = new Random(); int tal;
    tal = slumpGenerator.Next(0, 1000);
    textBox.Text = tal.ToString();
}

You have to set a minimum and a maximun value you want it to generate. You also have to put the minimum value 1 below what you really want to generate. i.e. if you want to generate a number between 10 and 20, you would need to put the minimum value to 9, and maximum value to 20.

You also need to put the value into a textbox, etc, to show it. Since it is an int, and textbox's text is in a String format, you will need to convert it to a String by putting this at the end of your code: .ToString()

I know this answer is late, but it might be able to help you later!

Upvotes: 1

Tom
Tom

Reputation: 26829

You can consider RNGCryptoServiceProvider thread safe class (System.Security.Cryptography namespace) which is cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider.

Implementation is a bit more difficult than using System.Random class.

Sample implementation is as follows:

using System.Security.Cryptography;
...

private RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();

private int NextInt32(int maxValue)
{
    byte[] intBytes = new byte[4];
    rnd.GetBytes(intBytes);
    return Math.Abs(BitConverter.ToInt32(intBytes, 0)) % maxValue + 1;
}

// And your method with textBox
private void button5_Click(object sender, EventArgs e)
{
    textBox.Text = NextInt32(1000).ToString(); 
}

You can read more on RNGCryptoServiceProvider in SO question: Pros and cons of RNGCryptoServiceProvider

Upvotes: 1

Seany84
Seany84

Reputation: 5596

private void button5_Click(object sender, EventArgs e)
{
    Random slumpGenerator = new Random(); 
    int tal = slumpGenerator.Next(0, 1000); 
    txtBxName.Text = tal.ToString();
}
  1. You need to add a miniumum and maximum to "Random" .Next() method.
  2. You are not setting the textboxes text value anywhere.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500485

Well sure - you're not setting any properties on your text box. You're ignoring your newly-generated random number. You'd need something like:

Random slumpGenerator = new Random();
// Or whatever limits you want... Next() returns a double
int tal = slumpGenerator.Next(0, 100);
textBox.Text = tal.ToString();

Note that in general it's a bad idea to create many Random instances - but it's not as simple as making it a static variable... see my article on randomness for more details. Also note how I've changed the code to declare a variable and assign it a value in a single statement - that's generally preferable to declaring in one statement and then assigning it a value later.

Upvotes: 6

Related Questions