473,406 Members | 2,698 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,406 software developers and data experts.

Problems with Random Numbers

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

For i = Weights.GetLowerBound(0) To Weights.GetUpperBound(0)
For j = Weights.GetLowerBound(1) To
Weights.GetUpperBound(1)
RandomNumber = RandomClass.Next()
Weights(i, j) = Convert.ToDouble((RandomNumber /
(Integer.MaxValue / 6)) - 3)
Next
Next

Aug 13 '06 #1
4 1329
Hi,

You need to seed your random numbers.

Dim RandomClass As New Random(now.ticks)

http://msdn2.microsoft.com/en-us/library/ctssatww.aspx

Ken
-----------------------

"Ricardo" wrote:
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

For i = Weights.GetLowerBound(0) To Weights.GetUpperBound(0)
For j = Weights.GetLowerBound(1) To
Weights.GetUpperBound(1)
RandomNumber = RandomClass.Next()
Weights(i, j) = Convert.ToDouble((RandomNumber /
(Integer.MaxValue / 6)) - 3)
Next
Next

Aug 13 '06 #2
not necessarily, you can also specifiy the lower bound (inclusive) and the
upper bound (non-inclusive)

Dim rnd As New Random
Dim intRnd as Integer = rnd.Next(0, 501)
that would get a random number between 0 and 500
--
-iwdu15
Aug 13 '06 #3
Iwdu15

And why is that than in any way better than using the Time?

What you show is fixed and every fixed part is always less randomized than
using the time now.

Just my idea,

Cor

"iwdu15" <jmmgoalsteratyahoodotcomschreef in bericht
news:85**********************************@microsof t.com...
not necessarily, you can also specifiy the lower bound (inclusive) and the
upper bound (non-inclusive)

Dim rnd As New Random
Dim intRnd as Integer = rnd.Next(0, 501)
that would get a random number between 0 and 500
--
-iwdu15

Aug 14 '06 #4
So as a mild musing, I made a shared class for making random numbers. Only
I am using the hashcode of a guid as my seed.

Public NotInheritable Class SimpleRandomizer

Private Shared pintSeed As Integer

Shared Sub New()
Dim g As Guid

g = System.Guid.NewGuid

pintSeed = g.GetHashCode

Randomize(CType(pintSeed, Double))
End Sub

Public Shared Function GetRandom(ByVal MaxValue As Integer, ByVal MinValue
As Integer) As Integer

'Test values
If MaxValue < 0 Then Throw New RandomizerException("Max value must be
greater than 0.", New RandomizerException.RandomizerInputs(pintSeed,
MaxValue, MinValue))
If MaxValue < 0 Then Throw New RandomizerException("Min value must be
greater than 0.", New RandomizerException.RandomizerInputs(pintSeed,
MaxValue, MinValue))
If MaxValue < MinValue Then Throw New RandomizerException("Min value
must be greater than 0.", New RandomizerException.RandomizerInputs(pintSeed,
MaxValue, MinValue))

Try
Return CInt(Int((MaxValue - MinValue + 1) * Rnd() + MinValue))
Catch ex As Exception
Throw New RandomizerException("Failed to generate random number", ex,
New RandomizerException.RandomizerInputs(pintSeed, MaxValue, MinValue))
End Try

End Function

Public Class RandomizerException
Inherits System.ApplicationException

Public Structure RandomizerInputs
Private pintSeed As Integer
Private pintUpperbound As Integer
Private pintLowerbound As Integer
Public Sub New(ByVal Seed As Integer, ByVal Upperbound As Integer,
ByVal Lowerbound As Integer)
pintSeed = Seed
pintUpperbound = Upperbound
pintLowerbound = Lowerbound
End Sub

Public ReadOnly Property Seed()
Get
Return pintSeed
End Get
End Property

Public ReadOnly Property Upperbound() As Integer
Get
Return pintUpperbound
End Get
End Property

Public ReadOnly Property Lowerbound() As Integer
Get
Return pintLowerbound
End Get
End Property

Public Overrides Function ToString() As String
Return String.Format("Seed: {0}, Upperbound: {1}, Lowerbound: {0}",
Seed, Upperbound, Lowerbound)
End Function

End Structure

Private pobjInputs As RandomizerInputs

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub

Public Sub New(ByVal message As String, ByVal innerException As
Exception)
MyBase.New(message, InnerException)
End Sub

Public Sub New(ByVal Inputs As RandomizerInputs)
MyBase.New()
pobjInputs = Inputs
End Sub

Public Sub New(ByVal message As String, ByVal Inputs As
RandomizerInputs)
MyBase.New(message)
pobjInputs = Inputs
End Sub

Public Sub New(ByVal message As String, ByVal innerException As
Exception, ByVal Inputs As RandomizerInputs)
MyBase.New(message, innerException)
pobjInputs = Inputs
End Sub

Public ReadOnly Property Inputs() As RandomizerInputs
Get
Return pobjInputs
End Get
End Property

Public Overrides Function ToString() As String
Return MyBase.ToString & vbCrLf & "Inputs:" & vbCrLf & Inputs.ToString
End Function

End Class

End Class
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uU**************@TK2MSFTNGP02.phx.gbl...
Iwdu15

And why is that than in any way better than using the Time?

What you show is fixed and every fixed part is always less randomized than
using the time now.

Just my idea,

Cor

"iwdu15" <jmmgoalsteratyahoodotcomschreef in bericht
news:85**********************************@microsof t.com...
>not necessarily, you can also specifiy the lower bound (inclusive) and
the
upper bound (non-inclusive)

Dim rnd As New Random
Dim intRnd as Integer = rnd.Next(0, 501)
that would get a random number between 0 and 500
--
-iwdu15


Aug 14 '06 #5

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

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
17
by: Gernot Frisch | last post by:
Hi, does repeatingly doing this: float num = GetRandomFloat(); for(;;) { float random = GetRandomFloat(); num*=random; num/=random;
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
10
by: Glenn Wilson | last post by:
I have a quick Question and I Hope some one can help or at least explain. What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I...
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...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
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...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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.