473,396 Members | 1,764 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.

Problems Generating Random Numbers

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 run this code the random numbers that are displayed are all the same for abot 25 cycles and then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module
Nov 20 '05 #1
10 1661
Environment.TickCount returns milliseconds, which aren't granular enough for the way you're going about this. So essentially you are creating multiple random numbers with the same seed, which of course means that they're not random at all.

You should change your tmpRandom to a private member of the class and initialize it only once. Then call tmpRandom.Next as you are doing now.
Nov 20 '05 #2
I beleive it has to do with the variable scope, if you create the variable
inside the function and initiate the value to environment.tick the SEED will
remain the same until the next tick, same seed causes same "random" number.
Try the same example with a module level random like this:

Module Module1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine
End Sub

Public Function RollPercent() As Integer
'Dim tmpRandom As New Random(Now.Millisecond)
Return tmpRandom.Next(1, 101)
End Function
End Module
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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 run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing
wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module

Nov 20 '05 #3
Thanks will give it a go

"Jared" wrote:
I beleive it has to do with the variable scope, if you create the variable
inside the function and initiate the value to environment.tick the SEED will
remain the same until the next tick, same seed causes same "random" number.
Try the same example with a module level random like this:

Module Module1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine
End Sub

Public Function RollPercent() As Integer
'Dim tmpRandom As New Random(Now.Millisecond)
Return tmpRandom.Next(1, 101)
End Function
End Module
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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 run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing
wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module


Nov 20 '05 #4
It's because the function calls are happening so fast that they're occurring
between tick counts. That's why you're getting duplicate values.

"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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 run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.
Can some one tell me why this is or at least tell me what I am doing wrong.
Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent) Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module

Nov 20 '05 #5
yEaH rIgHt,
Try the example for yourself. I realize what you are saying and I too
beleive this, but, I think it has to do with the scope of the variable. If
you instantiate the random generator inside the function the tick count may
be very well be the same for may cycles, resulting in duplicate numbers.
That is way I said if you instantiate the variable at a different level the
SEED has already been assigned resulting in a "non-duplicating" random
number. If I'm way off, someone please correct me.
Jared
"yEaH rIgHt" <nospam@haha> wrote in message
news:10*************@corp.supernews.com...
It's because the function calls are happening so fast that they're
occurring
between tick counts. That's why you're getting duplicate values.

"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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 run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing

wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop,

RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module


Nov 20 '05 #6
Declare the Random as static and only create it once.

Public Function RollPercent() As Integer
Static tmpRandom As Random
'Random uses time based seed by default so no need to specify.
If tmpRandom Is Nothing Then tmpRandom = New Random()
Return tmpRandom
End Function
--
Mick Doherty
http://homepage.ntlworld.com/mdaudi1...nate/home.html
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004
Nov 20 '05 #7
I don't think I am explaining this right. I am trying to say that when Glenn
was creating the variable inside the function the seed value was the same,
causing duplicate numbers. This was all relitave to the scope of the
variable in THIS example. I am not saying the only way to do this is make a
module level variable. I need to quit trying to help people, I am no expert,
and, instead of building on a post people seem to try to "one-up" or
"out-do" another poster; there are many ways to do any single task. My way
is not better, I was only explaining why I believed the value was
duplicating at the request in the original post. Everyone else is just
giving examples of how to correct the problem, which is fine, but if Glen
doesn't know what he is doing "wrong" how can he prevent this in the future?
"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:ey**************@TK2MSFTNGP11.phx.gbl...
Declare the Random as static and only create it once.

Public Function RollPercent() As Integer
Static tmpRandom As Random
'Random uses time based seed by default so no need to specify.
If tmpRandom Is Nothing Then tmpRandom = New Random()
Return tmpRandom
End Function
--
Mick Doherty
http://homepage.ntlworld.com/mdaudi1...nate/home.html
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004

Nov 20 '05 #8
Of course it was the way he was seeding the Random class that was causing
the problem. I wasn't disputing that. Take a couple of deep breaths. It's
going to be o.k.
"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
I don't think I am explaining this right. I am trying to say that when Glenn was creating the variable inside the function the seed value was the same,
causing duplicate numbers. This was all relitave to the scope of the
variable in THIS example. I am not saying the only way to do this is make a module level variable. I need to quit trying to help people, I am no expert, and, instead of building on a post people seem to try to "one-up" or
"out-do" another poster; there are many ways to do any single task. My way
is not better, I was only explaining why I believed the value was
duplicating at the request in the original post. Everyone else is just
giving examples of how to correct the problem, which is fine, but if Glen
doesn't know what he is doing "wrong" how can he prevent this in the future?

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in message news:ey**************@TK2MSFTNGP11.phx.gbl...
Declare the Random as static and only create it once.

Public Function RollPercent() As Integer
Static tmpRandom As Random
'Random uses time based seed by default so no need to specify.
If tmpRandom Is Nothing Then tmpRandom = New Random()
Return tmpRandom
End Function
--
Mick Doherty
http://homepage.ntlworld.com/mdaudi1...nate/home.html
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004


Nov 20 '05 #9
I was not disagreeing with you. I just posted an alternative solution. I did
not need to explain the cause of the problem since you had already done
that.

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi1...nate/home.html
"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
I don't think I am explaining this right. I am trying to say that when Glenn was creating the variable inside the function the seed value was the same,
causing duplicate numbers. This was all relitave to the scope of the
variable in THIS example. I am not saying the only way to do this is make a module level variable. I need to quit trying to help people, I am no expert, and, instead of building on a post people seem to try to "one-up" or
"out-do" another poster; there are many ways to do any single task. My way
is not better, I was only explaining why I believed the value was
duplicating at the request in the original post. Everyone else is just
giving examples of how to correct the problem, which is fine, but if Glen
doesn't know what he is doing "wrong" how can he prevent this in the

future?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004
Nov 20 '05 #10
Sorry, had a bad week, feeling a little hostile.

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:e3**************@TK2MSFTNGP11.phx.gbl...
I was not disagreeing with you. I just posted an alternative solution. I
did
not need to explain the cause of the problem since you had already done
that.

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi1...nate/home.html
"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
I don't think I am explaining this right. I am trying to say that when

Glenn
was creating the variable inside the function the seed value was the
same,
causing duplicate numbers. This was all relitave to the scope of the
variable in THIS example. I am not saying the only way to do this is make

a
module level variable. I need to quit trying to help people, I am no

expert,
and, instead of building on a post people seem to try to "one-up" or
"out-do" another poster; there are many ways to do any single task. My
way
is not better, I was only explaining why I believed the value was
duplicating at the request in the original post. Everyone else is just
giving examples of how to correct the problem, which is fine, but if Glen
doesn't know what he is doing "wrong" how can he prevent this in the

future?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004

Nov 20 '05 #11

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

Similar topics

7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
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...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.