473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Random Generator for nine numbers

Hi,

I'm trying to generate nine random numbers in visual c# 2005 for a windows application. No input will be needed from the user. All that is needed is the nine random integers. Any help will be appreciatied
Jul 9 '07 #1
15 1610
RedSon
5,000 Expert 4TB
What have you done so far? You should just be able to do something like Random.nextInt and keep getting more ints.
Jul 9 '07 #2
Dim MyInteger As Integer
Randomize
MyInteger = CInt(Int((9 * Rnd()) + 1))
Jul 10 '07 #3
kenobewan
4,871 Expert 4TB
What problem or error are you getting?
Jul 10 '07 #4
The problem that I am getting is that I want to generate nine integer numbers
example: 028492992
292402012
etc.

When I tried to use the random method in c# , I am not getting nine random integers. I am getting an error.

private void random_drivers_license(int min, int max, string license)
{
max++;
for(int ct=0; ct<9; ct++)

StringBuilder builder = new StringBuilder();
System.Random Randomnum = new System.Random();
int random_num = Randomnum.Next(min, max);

}

Can someone please help?
Jul 10 '07 #5
TRScheel
638 Expert 512MB
The code might be running too fast. Use one instance of the Random class, like this:

Expand|Select|Wrap|Line Numbers
  1. private static string Random_Drivers_License(int min, int max)
  2. {
  3.     string license = string.Empty;
  4.     Random rnd = new Random();
  5.  
  6.     for (int i = 0; i < 9; i++)
  7.     {
  8.         license += rnd.Next(min, max + 1).ToString();
  9.     }
  10.  
  11.     return license;
  12. }
  13.  
Jul 10 '07 #6
Thanks, for your help. Do I have to include a seed with my random function to make sure that the values don't repeat when executed more than once? Because I want to generate nine different numbers each time at runtime.

The code might be running too fast. Use one instance of the Random class, like this:

Expand|Select|Wrap|Line Numbers
  1. private static string Random_Drivers_License(int min, int max)
  2. {
  3.     string license = string.Empty;
  4.     Random rnd = new Random();
  5.  
  6.     for (int i = 0; i < 9; i++)
  7.     {
  8.         license += rnd.Next(min, max + 1).ToString();
  9.     }
  10.  
  11.     return license;
  12. }
  13.  
Jul 10 '07 #7
TRScheel
638 Expert 512MB
Thanks, for your help. Do I have to include a seed with my random function to make sure that the values don't repeat when executed more than once? Because I want to generate nine different numbers each time at runtime.
It seeds with time if you dont include a value, but if you create the random number generator in the loop, chances are the loop goes so fast each creation uses the same seed and thus creates the same value.
Jul 10 '07 #8
It seeds with time if you dont include a value, but if you create the random number generator in the loop, chances are the loop goes so fast each creation uses the same seed and thus creates the same value.
Ok, so it is best to only create one instance of the random function outside the loop. i tried to run the method and it didn't return any results? What could be the cause of this?
Jul 10 '07 #9
TRScheel
638 Expert 512MB
Ok, so it is best to only create one instance of the random function outside the loop. i tried to run the method and it didn't return any results? What could be the cause of this?
Your method or mine?
Jul 10 '07 #10
Your method or mine?
Your method didn't work when I tried it.
Jul 10 '07 #11
TRScheel
638 Expert 512MB
Your method didn't work when I tried it.
Can you post your implementation of it?

How you called it, and the class surrounding it? In addition, what references that class is using?
Jul 10 '07 #12
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Data.Sql;
  10. using System.Data.SqlClient;
  11. using System.Data.SqlTypes;
  12. using System.Data.OleDb;
  13. using Microsoft.SqlServer.Server;
  14. using System.Security.Cryptography;
  15.  
  16. namespace Project_3
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void label1_Click(object sender, EventArgs e)
  26.         {
  27.  
  28.         }
  29.  
  30.         private void textBox1_TextChanged(object sender, EventArgs e)
  31.         {
  32.  
  33.         }
  34.  
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.  
  38.         SqlConnection myConnection = new SqlConnection();
  39.               // System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
  40.         textBox1.Text = "Connected!";
  41.           //  myConnection.ConnectionString = "Server=" + textBox1.Text + ";User ID=;  PWD=; database="; 
  42.         //myConnection.ConnectionString = "Server=";Database=myDatabase;User ID=myUsername;Password=myPassword;Trusted_Connection=False; Encrypt=true; Connect Timeout=15";
  43.         myConnection.ConnectionString = "Data Source=" + textBox1.Text + ";User ID=;" + "PWD=";
  44.             SqlCommand thisCommand=myConnection.CreateCommand();
  45.  
  46.             try
  47.                {
  48.                     myConnection.Open();
  49.  
  50.  
  51.                 //get databases
  52.                     thisCommand.CommandType = CommandType.StoredProcedure;
  53.                     thisCommand.CommandText = "sp_databases";
  54.                 //get tables
  55.                     DataTable tables = null;
  56.                     tables = myConnection.GetSchema("Tables");
  57.  
  58.                 //find all columns in tables
  59.                     SqlPipe p;
  60.                     SqlCommand sql = new SqlCommand();
  61.  
  62.                     sql.CommandText = "SELECT a.column_name, a.data_type, a.character_maximum_length, a.numeric_precision, a.numeric_scale" +
  63.                         "FROM information_schema.columns a INNER JOIN information_schema.tables t ON a.table_name=t.table_name" +
  64.                         "WHERE a.table_name LIKE @search AND a.column_name LIKE @search1 AND t.table_type='BASE TABLE'" + "ORDER BY a.column_name";
  65.                 p=SqlContext.Pipe;
  66.                 p.ExecuteAndSend(sql);
  67.  
  68.                 //find column information
  69.                 }
  70.                catch (Exception ex)
  71.                 {   
  72.  
  73.                     MessageBox.Show("Failed to connect!");
  74.                     MessageBox.Show("Could not connect to database, please try again!");
  75.                     Application.Restart();
  76.  
  77.                    // create dataset to hold rows, columns, etc
  78.                    DataSet dataset = new DataSet();
  79.  
  80.                 }               
  81.                 finally
  82.                 {
  83.                     myConnection.Close();
  84.                 }
  85.                 Random_Drivers_License(1, 0xFFFFFF);
  86.         }
  87.         private static string Random_Drivers_License(int min, int max)
  88.         {
  89.             string license = string.Empty;
  90.             Random rnd = new Random();
  91.  
  92.             for (int i = 0; i < 9; i++)
  93.             {
  94.                 license += rnd.Next(min, max + 1).ToString();
  95.             }
  96.  
  97.             return license;
  98.         }
  99.  
  100.         }
  101.         }
  102.  
  103. //class1.cs
  104. using System;
  105. using System.Collections.Generic;
  106. using System.Text;
  107. using System.Net;
  108. using System.IO;
  109.  
  110. namespace Project_3
  111. {
  112.     class Class1
  113.     {
  114.         static void main(string[] args)
  115.         {
  116.             StringBuilder str = new StringBuilder();
  117.             byte[] buffer = new byte[65535];
  118.  
  119.             //request page
  120.             HttpWebRequest request = (HttpWebRequest)
  121.             WebRequest.Create("http://www.census.gov/genealogy/names/dist.female.first");
  122.  
  123.             //execute web page
  124.             HttpWebResponse response =(HttpWebResponse)
  125.             request.GetResponse();
  126.  
  127.             //read data from web page
  128.             Stream read_stream = response.GetResponseStream();
  129.  
  130.             string temp = null;
  131.             int ct = 0;
  132.  
  133.             do
  134.             {
  135.                 //fill buffer
  136.                 ct = read_stream.Read(buffer, 0, buffer.Length);
  137.                 if (ct != 0)
  138.                 {
  139.                     temp = Encoding.ASCII.GetString(buffer, 0, ct);
  140.  
  141.                     //append
  142.                     str.Append(temp);
  143.                 }
  144.             } while (ct > 0);
  145.  
  146.         }
  147.     }
  148. }
  149.  
  150. //references
  151. ADODB
  152. ADOX
  153. BrowseUI
  154. Interoop.SQLDMO
  155. MMCInternalWebOcx
  156. MSDASC
  157. MSHTML
  158. MSOLAPADMINLib2
  159. MSOLAPUI80
  160. SQLNS
  161. SYSTEM
  162. System.Data
  163. System.XML
  164.  
  165.  
Jul 10 '07 #13
RedSon
5,000 Expert 4TB
I spent about 10 minutes fixing and formatting your code and then finally gave up. Please be more considerate of others by being sure to use CODE tags and also formatting your code without hundreds of newlines in between the code blocks.
Jul 10 '07 #14
TRScheel
638 Expert 512MB
You arent doing anything with the return result. You are essentially making the function idempotent.

Expand|Select|Wrap|Line Numbers
  1. Random_Drivers_License(1, 0xFFFFFF);
  2.  
Calling it like that just calls it. It does nothing with the return value, and I could call it a million times and get no result.

Expand|Select|Wrap|Line Numbers
  1. string result = string.Empty;
  2. result = Random_Drivers_License(1, 0xFFFFFF);
  3.  
Now you have result. The variable 'result' will contain the return value of the function in question. You can now use that 'result' variable to do something.


A couple notes on your code. You will praise <insert your diety of choice here> a few years from now when you come back to look at your code if you use a good naming convention. Textbox1 is a bad way to start naming things. txtbxStatus is a better name. Thats just an example. Go through each object and do that. In addition, store you connection strings in a web.Config or resource file(I assume resource file as this seems to be a desktop application) that way they are uniform and not copy/pasted in your code everytime you need it. Rule of thumb, embedded strings are bad.
Jul 11 '07 #15
[Thanks, I'll give that a try.

QUOTE=TRScheel]You arent doing anything with the return result. You are essentially making the function idempotent.

Expand|Select|Wrap|Line Numbers
  1. Random_Drivers_License(1, 0xFFFFFF);
  2.  
Calling it like that just calls it. It does nothing with the return value, and I could call it a million times and get no result.

Expand|Select|Wrap|Line Numbers
  1. string result = string.Empty;
  2. result = Random_Drivers_License(1, 0xFFFFFF);
  3.  
Now you have result. The variable 'result' will contain the return value of the function in question. You can now use that 'result' variable to do something.


A couple notes on your code. You will praise <insert your diety of choice here> a few years from now when you come back to look at your code if you use a good naming convention. Textbox1 is a bad way to start naming things. txtbxStatus is a better name. Thats just an example. Go through each object and do that. In addition, store you connection strings in a web.Config or resource file(I assume resource file as this seems to be a desktop application) that way they are uniform and not copy/pasted in your code everytime you need it. Rule of thumb, embedded strings are bad.[/quote]
Jul 11 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
20
by: Levi Campbell | last post by:
Hi, I'm working on a random number generator using the internet as a way to gather entropy, I have two questions. 1. is there a way to capture the internet stream? 2. how would I skip every 2nd,...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
5
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag =...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.