473,396 Members | 2,039 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 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 1761
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******@4guysfromrolla.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******@4guysfromrolla.com> wrote in message
news:41**************@4guysfromrolla.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******@4guysfromrolla.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**************@TK2MSFTNGP11.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******@4guysfromrolla.com> wrote in message
news:41**************@4guysfromrolla.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******@4guysfromrolla.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
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...
3
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
by: abhi | last post by:
How do you get random numbers in vb .net? I want random six digit numbers. Cheers, Abhi
4
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...
12
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...
3
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: *************************************************...
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...
11
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...
20
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,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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...
0
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,...

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.