473,666 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Integer

How can I choose a random integer between to given integers?

For example, I'd like to pick a random integer between 1 and 3. E.g. 1, 2,
or 3

If I do

Dim oRandom As New Random(1)
Response.Write oRandom.Next(3)

will I get a different number between 1 and 3 every time, even if I'm always
using 1 as a seed?

Thanks!
Nov 18 '05 #1
3 1788
George Durzi wrote:
How can I choose a random integer between to given integers?

For example, I'd like to pick a random integer between 1 and 3. E.g. 1, 2,
or 3

If I do

Dim oRandom As New Random(1)
Response.Write oRandom.Next(3)

will I get a different number between 1 and 3 every time, even if I'm always
using 1 as a seed?


For a given seed, you'll always get the same sequence of "random"
numbers. So, yes, if you always use the seed 1, the sequence of numbers
..Next will return will be the same.

If you want a more randomized experience, simply omit the seed. The
Random class will cause a seed obtained from the system clock to be
used, so it should produce different sequences of numbers each time.

hth

--

Scott Mitchell
mi******@4guysf romrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla. com!
Nov 18 '05 #2
Thanks Scott, but without the seed, is there a way to ensure the number is
between, for example, 1 and 3? Or will it grab a random number between 0 and
3?

"Scott Mitchell [MVP]" <mi******@4guys fromrolla.com> wrote in message
news:41******** ******@4guysfro mrolla.com...
George Durzi wrote:
How can I choose a random integer between to given integers?

For example, I'd like to pick a random integer between 1 and 3. E.g. 1,
2, or 3

If I do

Dim oRandom As New Random(1)
Response.Write oRandom.Next(3)

will I get a different number between 1 and 3 every time, even if I'm
always using 1 as a seed?


For a given seed, you'll always get the same sequence of "random" numbers.
So, yes, if you always use the seed 1, the sequence of numbers .Next will
return will be the same.

If you want a more randomized experience, simply omit the seed. The
Random class will cause a seed obtained from the system clock to be used,
so it should produce different sequences of numbers each time.

hth

--

Scott Mitchell
mi******@4guysf romrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla. com!

Nov 18 '05 #3
The seed = 1 does not mean your random number starts with 1. If you want to
get an integer between 1 - 3 just use Next(int minValue, int maxValue)

ie:
Random random = new Random();
int result = random.Next(1, 3);

--
Victor Hadianto
http://synop.com/Products/SauceReader/

"George Durzi" <gd****@hotmail .com> wrote in message
news:uD******** ******@TK2MSFTN GP11.phx.gbl...
Thanks Scott, but without the seed, is there a way to ensure the number is
between, for example, 1 and 3? Or will it grab a random number between 0
and 3?

"Scott Mitchell [MVP]" <mi******@4guys fromrolla.com> wrote in message
news:41******** ******@4guysfro mrolla.com...
George Durzi wrote:
How can I choose a random integer between to given integers?

For example, I'd like to pick a random integer between 1 and 3. E.g. 1,
2, or 3

If I do

Dim oRandom As New Random(1)
Response.Write oRandom.Next(3)

will I get a different number between 1 and 3 every time, even if I'm
always using 1 as a seed?


For a given seed, you'll always get the same sequence of "random"
numbers. So, yes, if you always use the seed 1, the sequence of numbers
.Next will return will be the same.

If you want a more randomized experience, simply omit the seed. The
Random class will cause a seed obtained from the system clock to be used,
so it should produce different sequences of numbers each time.

hth

--

Scott Mitchell
mi******@4guysf romrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla. com!


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2494
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
3
2187
by: tshad | last post by:
Is there a good random number/letter generator out there? I just want to be able to generate a password that has letters and or numbers about 10 characters long. Thanks, Tom
9
8376
by: abhi | last post by:
How do you get random numbers in vb .net? I want random six digit numbers. Cheers, Abhi
4
1345
by: Ricardo | last post by:
Hi wizards, I have an object , this object generate a set of 9 random numbers. When I generate for example 10 object his 9 random numbers are equals. How can I make this 9 numbers are different for each object I use this code in Sub New : Dim RandomNumber As Integer Dim RandomClass As New Random
12
3217
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
3
2076
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: ************************************************* Function RandomString(size as integer, lowerCase as boolean) as string Dim builder as StringBuilder = new StringBuilder() Dim random as Random = new Random() Dim i as integer dim ch as char
6
11741
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 that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
11
3017
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays of values. I get a random index value for each array so I can pull the data from them.
20
2276
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38, 39, 40, 41, 42, 43}. Also, I read in my compiler's documentation the following: To get a random number in the range 0..N, use rand()%(N+1). Note that the low bits of the rand's return value are not very random, so rand()%N for small values of N...
0
8355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.