生成随机字符串到文本框(内部代码) [英] Generate random string into text box (code inside)

查看:78
本文介绍了生成随机字符串到文本框(内部代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要帮助我的队列号生成器。



基本上,我需要生成一个5个字符的字符串,前面带字母'A'然后是4个随机数



例如:



A6932

A1038

A0193



Hi guys, I need help with my queue number generator.

Basically, i need to generate a 5 character string with the letter 'A' at the front followed by 4 random numbers

example:

A6932
A1038
A0193

Random random = new Random();
int length = 5;

for (int i = 0; i < length; i++)
{
	//if random.Next() == 0 then we generate a random character
	if (random.Next(0, 3) == 0)
	{
		tb_queuenum.Text += ((char)random.Next(65, 91)).ToString();
	}
	else //if random.Next() == 0 then we generate a random digit
	{
		tb_queuenum.Text += random.Next(0, 9);
	}
}





这些是我的代码,任何帮助都将深受赞赏< 3

如果有更好的替代我的代码,请分享。

上帝保佑。



These are my codes right now, any help will be deeply appreciated <3
If there's a better alternative to my codes please share.
God bless.

推荐答案

不确定到底是什么你的问题是,但你确实问过是否有更好的方法来实现这个目标。



Not sure what exactly your issue is, but you did ask if there was a better way to achieve this.

int min = 0;
int max = 9999;

Random random = new Random();
int randomInt = random.Next(min, max);

var randomValue = String.Format("A{0:D4}", randomInt);


这篇关于生成随机字符串到文本框(内部代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆