帮助随机算法 [英] Help with Random Algorithm

查看:49
本文介绍了帮助随机算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,

需要帮助这个随机排序算法


private void cmdQuestion_Click(object sender,System.EventArgs e)

{

随机rnd = new Random(); //初始化rnd到新的随机对象

System.Random iRnd = new System.Random();

string theNum = iRnd.Next(0,8).ToString( );


lblAnswer.Text = iRnd.Next(0,8).ToString();


这给我一个随机数然而,我需要将数字序列转换为字符串值,将索引传递给随机对象

并对数组或if if解析索引语句或开关。

有许多控制台输出示例,但没有Web应用程序的示例

输出。任何建议。


谢谢


Johnny

Hey guys,
Need help with this random sort algorithm

private void cmdQuestion_Click(object sender, System.EventArgs e)
{
Random rnd = new Random(); //initialize rnd to new random object
System.Random iRnd = new System.Random();
string theNum = iRnd.Next(0,8).ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString();

This gives me a random number upon the click event however I need to turn
the numeric sequence into a string value, pass an index to the random object
and parse the index against an array or an if else statement or switch.
Have many examples for a console output but none for a web application
output. any suggestions.

Thanks

Johnny

推荐答案

Johnny Snead< ca **** @ gte.net>写道:
Johnny Snead <ca****@gte.net> wrote:
private void cmdQuestion_Click(object sender,System.EventArgs e)
随机rnd = new Random(); //初始化rnd到新的随机对象
System.Random iRnd = new System.Random();


为什么你想要*两个*随机实例?


另外,你应该存储对单个实例的引用并重复

使用它 - 否则,如果你在*非常*快速连续中获得两次点击

(在这种情况下不太可能,不可否认 - 这是更一般的原则)

你可能得到相同的数字,因为当前时间使用的种子是



string theNum = iRnd.Next(0,8)。 ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString();

这给我一个随机数字点击事件,但我需要将数字序列转换为字符串值,将索引传递给随机对象
并针对数组或if else语句或开关解析索引。
有很多控制台示例输出但没有用于Web应用程序
输出。
private void cmdQuestion_Click(object sender, System.EventArgs e)
{
Random rnd = new Random(); //initialize rnd to new random object
System.Random iRnd = new System.Random();
Why do you want *two* instances of Random?

Also, you should store a reference to a single instance and repeatedly
use that - otherwise if you get two clicks in *very* quick succession
(unlikely in this case, admittedly - this is more a general principle)
you may well get the same numbers out, as the seed used by Random is
the current time.
string theNum = iRnd.Next(0,8).ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString();

This gives me a random number upon the click event however I need to turn
the numeric sequence into a string value, pass an index to the random object
and parse the index against an array or an if else statement or switch.
Have many examples for a console output but none for a web application
output.




好​​吧,你已经把数字转换为字符串值 - 但是什么

哟你的意思是将一个索引传递给随机对象?


如果你能在每个阶段给出你想要的完整例子,那么它将是b $ b帮助。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet/

如果回复小组,请不要给我发邮件



Well, you''ve done the turning the number into a string value - but what
do you mean by "pass an index to the random object"?

If you could give a full example of what you want at each stage it
would help.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Jon,

谢谢

我想赚个便宜版本的魔法8球与随机说法

来自案例陈述或if - else陈述。我是VB6学生

试图跳转到C#。

我有一个随机数的排序和输出现在我需要更改

这是一个字符串的随机排序,只有一个输出到一个标签。所有

这是在一个网络应用程序。

和你的权利我不需要两个随机的实例我没有抓住

,直到你说某事。

" Jon Skeet" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ news.microsoft.com ...
Jon,
Thanks
I am trying to make a cheap version of magic 8 ball with the random sayings
from either a case statement or an if - else statement. I am a VB6 student
trying to jump to C#.
I have the sort and output down for a random number now I need to change
this to a random sort on a string with a single output into a lable. All
this is in a web application.
And your right I dont need two instances of random I did not catch that
until you said something.
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Johnny Snead< ca **** @ gte.net>写道:
Johnny Snead <ca****@gte.net> wrote:
private void cmdQuestion_Click(object sender,System.EventArgs e)
随机rnd = new Random(); //初始化rnd到新的随机对象
System.Random iRnd = new System.Random();
为什么你想要*两个* Random实例?你的错误我的错误

此外,你应该存储一个单一实例的引用并反复使用它 - 否则如果你在*非常*快速连续中获得两次点击
在这种情况下,诚然 - 这是一般原则)
你可能会得到相同的数字,因为Random使用的种子是当前时间。
private void cmdQuestion_Click(object sender, System.EventArgs e)
{
Random rnd = new Random(); //initialize rnd to new random object
System.Random iRnd = new System.Random();
Why do you want *two* instances of Random? Your right my mistake

Also, you should store a reference to a single instance and repeatedly
use that - otherwise if you get two clicks in *very* quick succession
(unlikely in this case, admittedly - this is more a general principle)
you may well get the same numbers out, as the seed used by Random is
the current time.
string theNum = iRnd.Next(0,8).ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString();这会输出一个带有数据类型字符串的数字
我想要一个字符串输出,我随机说要使

8球完成。
这给了我一个随机数字点击事件但是我需要
将数字序列转换为字符串值,将索引传递给随机
对象并针对数组或if else语句或开关解析索引。
有很多示例用于控制台输出但没有用于Web应用程序
输出。
string theNum = iRnd.Next(0,8).ToString();

lblAnswer.Text = iRnd.Next(0,8).ToString(); This outputs a number with a datatype string I want a string output with my random saying to make
the 8 ball complete.
This gives me a random number upon the click event however I need to turn the numeric sequence into a string value, pass an index to the random object and parse the index against an array or an if else statement or switch.
Have many examples for a console output but none for a web application
output.



嗯,你已经完成了将数字转换为字符串值 - 但是什么
你的意思是将一个索引传递给随机对象?



Well, you''ve done the turning the number into a string value - but what
do you mean by "pass an index to the random object"?




我试图在我的排序方法中做得更多。通过一个案例结构而不是仅仅通过一个案例结构来考虑传递对象的引用

并让该对象生成随机字符串。 (看着太多Dan

Ingalls视频我猜)
如果你能在每个阶段给出你想要的完整例子,它将会有所帮助。
希望这个详细解释了我正在做的事情,感谢您快速回复
。你们很快!!


再次感谢


Johnny -
Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet/
如果回复小组,请不要给我发邮件



I was trying to be more OO in my approach to the sort. Instead of just
going thru a case structure was thinking of passing a reference to an object
and letting that object generate the random string. (Watching too much Dan
Ingalls videos I guess)
If you could give a full example of what you want at each stage it
would help.
Hope this explains more about what I am doing and I appreciate your quick response. You guys are fast !!

Thanks again

Johnny --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Johnny Snead< ca **** @ gte达网络>写道:
Johnny Snead <ca****@gte.net> wrote:
我试图从案例陈述或if - else语句中随机说法制作一个廉价版魔术8球。我是一名VB6学生
试图跳转到C#。
我有一个随机数的排序和输出现在我需要改变
这个随机排序一个单一的字符串输出到标签。所有
这都是在一个Web应用程序中。
I am trying to make a cheap version of magic 8 ball with the random sayings
from either a case statement or an if - else statement. I am a VB6 student
trying to jump to C#.
I have the sort and output down for a random number now I need to change
this to a random sort on a string with a single output into a lable. All
this is in a web application.




我强烈建议您先将它作为控制台应用程序执行

- 它会让它更简单,所以你可以专注于

a时间的一个问题。现在,为什么你需要对字符串进行随机排序?什么

确切*是*随机排序?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet /

如果回复小组,请不要给我发邮件



I would strongly suggest that you do it as a console application first
- it''ll make it simpler, so you can concentrate on just one problem at
a time. Now, why do you need to do a "random sort on a string"? What
exactly *is* a random sort anyway?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


这篇关于帮助随机算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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