8 Replies - 2941 Views - Last Post: 18 November 2009 - 10:17 AM Rate Topic: -----

#1 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Random Number Generator (Complicated)(For me)

Post icon  Posted 16 November 2009 - 09:39 PM

Hello,
I have to create a program that generates as many random numbers as the user selects in a "numeric up and down" control object and displays the random numbers in a list box.
I know that I must use nested loops and a random number Generator and I was able to make a list of numbers appear in the listbox but not any random numbers.
I was able to get the numeric up and down involved in dictating the amount of numbers that show up in the list box.
the only problem I'm having is making the numbers random.
my code is:

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RandomNumberGenerator
{
	public partial class frmRandomNumbers : Form
	{
		public frmRandomNumbers()
		{
			InitializeComponent();
		}



		private void btnGenerate_Click(object sender, EventArgs e)
		{
			//VAliables are declared
			int FirstNumber;
			int aRandomNumber;
			
			//Numbers to show up in the list box
			this.lstNumbers.Items.Clear();
			for (aRandomNumber = 0; aRandomNumber <= numudNumberOfRandoms.Value; aRandomNumber++)
			{
				for (FirstNumber = 0; FirstNumber <= 0; FirstNumber++)

					this.lstNumbers.Items.Add(FirstNumber + " -> " + aRandomNumber);
	}
}


   }
	}



How do you make the numbers Random???

Is This A Good Question/Topic? 0
  • +

Replies To: Random Number Generator (Complicated)(For me)

#2 knichols  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 21
  • Joined: 09-November 09

Re: Random Number Generator (Complicated)(For me)

Posted 16 November 2009 - 09:49 PM

x here would be a random number between 0 and 25.
Keep calling next to get a new random number.
It also supports using a seed.




Random Rand = new Random();
int x = Rand.Next(0, 25);




Was This Post Helpful? 1
  • +
  • -

#3 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Re: Random Number Generator (Complicated)(For me)

Posted 17 November 2009 - 05:12 PM

What do you mean by calling next?
how do you use that bit of code?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Random Number Generator (Complicated)(For me)

Posted 17 November 2009 - 05:24 PM

Random is a class. You create a new instance of the class using the new keyword like any other object. The Next method of the Random class is used to generate random numbers. What you would want to do is inside the Click event for your button is create a new instance of the Random class. Then inside your inner loop in the nested loops you would generate a random number using the Next method, if you want an integer value, or NextDouble if you want a double. There is also a NextBytes method but I won't go into that. The Next method has two overloads. The first one has one parameter, an int and it will return a value less than the parameter and greater than zero, this value must not be negative. The other overload of the Next method takes two parameters. The first number is the lower bound of the range of numbers you want to generate and the second is the upper bound of the range of numbers you want to generate excluding the number itself. NextDouble takes no parameters and returns a double between 0 and 1.0.

In your code you would want to do something like this:

		private void btnGenerate_Click(object sender, EventArgs e)
		{
			//VAliables are declared
			int FirstNumber;
			int aRandomNumber;
			Random randomGenerator = new Random();
		   
			//Numbers to show up in the list box
			this.lstNumbers.Items.Clear();
			for (aRandomNumber = 0; aRandomNumber <= numudNumberOfRandoms.Value; aRandomNumber++)
			{
				for (FirstNumber = 0; FirstNumber <= 0; FirstNumber++)
				{
					int number = randomGenerator.Next(0, 26);
					this.lstNumbers.Items.Add(FirstNumber + " -> " + number);
				}
			 }



Now, that will generate random numbers between 0 and 25, since like I said above 26 will not be included in the numbers generated. I hope that helps you. :)

*edit*
Made a typo. Sorry.

This post has been edited by SixOfEleven: 17 November 2009 - 05:26 PM

Was This Post Helpful? 1
  • +
  • -

#5 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Re: Random Number Generator (Complicated)(For me)

Posted 17 November 2009 - 05:55 PM

View PostSixOfEleven, on 17 Nov, 2009 - 04:24 PM, said:

Random is a class. You create a new instance of the class using the new keyword like any other object. The Next method of the Random class is used to generate random numbers. What you would want to do is inside the Click event for your button is create a new instance of the Random class. Then inside your inner loop in the nested loops you would generate a random number using the Next method, if you want an integer value, or NextDouble if you want a double. There is also a NextBytes method but I won't go into that. The Next method has two overloads. The first one has one parameter, an int and it will return a value less than the parameter and greater than zero, this value must not be negative. The other overload of the Next method takes two parameters. The first number is the lower bound of the range of numbers you want to generate and the second is the upper bound of the range of numbers you want to generate excluding the number itself. NextDouble takes no parameters and returns a double between 0 and 1.0.

In your code you would want to do something like this:

		private void btnGenerate_Click(object sender, EventArgs e)
		{
			//VAliables are declared
			int FirstNumber;
			int aRandomNumber;
			Random randomGenerator = new Random();
		   
			//Numbers to show up in the list box
			this.lstNumbers.Items.Clear();
			for (aRandomNumber = 0; aRandomNumber <= numudNumberOfRandoms.Value; aRandomNumber++)
			{
				for (FirstNumber = 0; FirstNumber <= 0; FirstNumber++)
				{
					int number = randomGenerator.Next(0, 26);
					this.lstNumbers.Items.Add(FirstNumber + " -> " + number);
				}
			 }



Now, that will generate random numbers between 0 and 25, since like I said above 26 will not be included in the numbers generated. I hope that helps you. :)

*edit*
Made a typo. Sorry.

You ARE A GENIUS!!!!
THANK YOU!!!
IT FINNALLY WORKS!!!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#6 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Re: Random Number Generator (Complicated)(For me)

Posted 17 November 2009 - 06:08 PM

One last question...What is an optional seed and how can I add one into this program?
Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Random Number Generator (Complicated)(For me)

Posted 17 November 2009 - 06:22 PM

For the Random class to work it requires a seed. A seed is a number that will start off the random number generation process. Random numbers are not truly random. They are created using a mathematical function. It is not like in real life where when you roll a die, flip a coin or pull a number out of a hat where those numbers are truly random. There are a few ways that you can generate a seed. One is to use the Environment.TickCount property and pass it in as the constructor like this:

Random randomGenerator = new Random(Environment.TickCount);



The reason for using Environment.TickCount is it will always be different each time the program is run. I believe if you were to pass in a value that does not change, like 25175, the generator will generate the same series of numbers. You need use a seed that will be different every time the program runs. Another commonly used seed is using DateTime.Now which will also be different each time the program runs.
Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Random Number Generator (Complicated)(For me)

Posted 17 November 2009 - 06:57 PM

The default Random() constructor uses a time based seed, there is no need to seed it yourself unless you are trying to generate the same Random sequence (usually for testing).
Was This Post Helpful? 0
  • +
  • -

#9 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: Random Number Generator (Complicated)(For me)

Posted 18 November 2009 - 10:17 AM

I use several kinds of RNG forms.

My favorite is simply keeping a running average and MD5sum of the mouse movement. Each time the FORM registers a mouse move, it adds the X and Y to the running sum, divides by the number of Ticks in DateTime.Now.Milliseconds and gets the MD5 value for that. Sum(bytes) from MD5 and you have a nearly perfect RNG.

Take any combo, but as long as you use something nondeterministic (CPU usage, Disk usage, Net I/O statistics, Mouse movement, keyboard movement, etc, something that is ultimately un-repeatable).

One of my favorite ways to seed the RNG for .NET? the Intel x86 RNG function twice: Once for offset in memory and then for length. Sum the bits, and you're nearly perfect.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1