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
15 1610
What have you done so far? You should just be able to do something like Random.nextInt and keep getting more ints.
Dim MyInteger As Integer
Randomize
MyInteger = CInt(Int((9 * Rnd()) + 1))
What problem or error are you getting?
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?
The code might be running too fast. Use one instance of the Random class, like this: -
private static string Random_Drivers_License(int min, int max)
-
{
-
string license = string.Empty;
-
Random rnd = new Random();
-
-
for (int i = 0; i < 9; i++)
-
{
-
license += rnd.Next(min, max + 1).ToString();
-
}
-
-
return license;
-
}
-
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: -
private static string Random_Drivers_License(int min, int max)
-
{
-
string license = string.Empty;
-
Random rnd = new Random();
-
-
for (int i = 0; i < 9; i++)
-
{
-
license += rnd.Next(min, max + 1).ToString();
-
}
-
-
return license;
-
}
-
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.
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?
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?
Your method or mine?
Your method didn't work when I tried it.
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?
- using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Runtime.InteropServices;
-
using System.Data.Sql;
-
using System.Data.SqlClient;
-
using System.Data.SqlTypes;
-
using System.Data.OleDb;
-
using Microsoft.SqlServer.Server;
-
using System.Security.Cryptography;
-
-
namespace Project_3
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void label1_Click(object sender, EventArgs e)
-
{
-
-
}
-
-
private void textBox1_TextChanged(object sender, EventArgs e)
-
{
-
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
-
SqlConnection myConnection = new SqlConnection();
-
// System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
-
textBox1.Text = "Connected!";
-
// myConnection.ConnectionString = "Server=" + textBox1.Text + ";User ID=; PWD=; database=";
-
//myConnection.ConnectionString = "Server=";Database=myDatabase;User ID=myUsername;Password=myPassword;Trusted_Connection=False; Encrypt=true; Connect Timeout=15";
-
myConnection.ConnectionString = "Data Source=" + textBox1.Text + ";User ID=;" + "PWD=";
-
SqlCommand thisCommand=myConnection.CreateCommand();
-
-
try
-
{
-
myConnection.Open();
-
-
-
//get databases
-
thisCommand.CommandType = CommandType.StoredProcedure;
-
thisCommand.CommandText = "sp_databases";
-
//get tables
-
DataTable tables = null;
-
tables = myConnection.GetSchema("Tables");
-
-
//find all columns in tables
-
SqlPipe p;
-
SqlCommand sql = new SqlCommand();
-
-
sql.CommandText = "SELECT a.column_name, a.data_type, a.character_maximum_length, a.numeric_precision, a.numeric_scale" +
-
"FROM information_schema.columns a INNER JOIN information_schema.tables t ON a.table_name=t.table_name" +
-
"WHERE a.table_name LIKE @search AND a.column_name LIKE @search1 AND t.table_type='BASE TABLE'" + "ORDER BY a.column_name";
-
p=SqlContext.Pipe;
-
p.ExecuteAndSend(sql);
-
-
//find column information
-
}
-
catch (Exception ex)
-
{
-
-
MessageBox.Show("Failed to connect!");
-
MessageBox.Show("Could not connect to database, please try again!");
-
Application.Restart();
-
-
// create dataset to hold rows, columns, etc
-
DataSet dataset = new DataSet();
-
-
}
-
finally
-
{
-
myConnection.Close();
-
}
-
Random_Drivers_License(1, 0xFFFFFF);
-
}
-
private static string Random_Drivers_License(int min, int max)
-
{
-
string license = string.Empty;
-
Random rnd = new Random();
-
-
for (int i = 0; i < 9; i++)
-
{
-
license += rnd.Next(min, max + 1).ToString();
-
}
-
-
return license;
-
}
-
-
}
-
}
-
-
//class1.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.Net;
-
using System.IO;
-
-
namespace Project_3
-
{
-
class Class1
-
{
-
static void main(string[] args)
-
{
-
StringBuilder str = new StringBuilder();
-
byte[] buffer = new byte[65535];
-
-
//request page
-
HttpWebRequest request = (HttpWebRequest)
-
WebRequest.Create("http://www.census.gov/genealogy/names/dist.female.first");
-
-
//execute web page
-
HttpWebResponse response =(HttpWebResponse)
-
request.GetResponse();
-
-
//read data from web page
-
Stream read_stream = response.GetResponseStream();
-
-
string temp = null;
-
int ct = 0;
-
-
do
-
{
-
//fill buffer
-
ct = read_stream.Read(buffer, 0, buffer.Length);
-
if (ct != 0)
-
{
-
temp = Encoding.ASCII.GetString(buffer, 0, ct);
-
-
//append
-
str.Append(temp);
-
}
-
} while (ct > 0);
-
-
}
-
}
-
}
-
-
//references
-
ADODB
-
ADOX
-
BrowseUI
-
Interoop.SQLDMO
-
MMCInternalWebOcx
-
MSDASC
-
MSHTML
-
MSOLAPADMINLib2
-
MSOLAPUI80
-
SQLNS
-
SYSTEM
-
System.Data
-
System.XML
-
-
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.
You arent doing anything with the return result. You are essentially making the function idempotent. -
Random_Drivers_License(1, 0xFFFFFF);
-
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. -
string result = string.Empty;
-
result = Random_Drivers_License(1, 0xFFFFFF);
-
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.
[Thanks, I'll give that a try.
QUOTE=TRScheel]You arent doing anything with the return result. You are essentially making the function idempotent. -
Random_Drivers_License(1, 0xFFFFFF);
-
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. -
string result = string.Empty;
-
result = Random_Drivers_License(1, 0xFFFFFF);
-
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]
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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 =...
|
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,...
|
by: chico_yallin |
last post by:
I just wana make a random id number based on4 digits-for examples??
Thanks in Advance
Ch.Yallin
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |