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.
Except the data is similar on every run.
I know its a timing issue because when I step through code, it gives me randoms.
However, is there anything faster than milliseconds that I can seed at?
Here's my code. Please let me know if you need me to explain anything. Basically, i'm creating a random math expression: -
#Region "Random Number Generator does not work"
-
Private Function RandomExpressionCreateOperand() As String
-
'Array of constants
-
Dim szConstants() As String = {"pi", "e"}
-
'Array of functions
-
Dim szFunctions() As String = {"Acos", "Asin", "Atan", "Ceiling", "Cos", "Cosh", "Floor", "Log", "Sin", "Sqrt", "Tan"}
-
'Array of parameters (proprietary)
-
Dim szParameters() As String = {"Parameter1", "Param.eu"}
-
'Array of tokens (corresponding to Constant, Fucntion, Parameter or raw value, respectively)
-
Dim iTokens() As Integer = {1, 2, 3, 4}
-
'Seed random number generator
-
Dim rc As New Random(Date.Now.Millisecond)
-
'Random number related to token (Constant, Function, Parameter or Value)
-
Dim iToken As Integer = rc.Next(1, UBound(iTokens))
-
'Return string
-
Dim szRet As String = ""
-
'Index of value in the array
-
Dim iIndex As Integer = 0
-
'Raw value but also parameter of a function token
-
Dim iParam As Integer = 0
-
'Max value of parameter or raw value:
-
Dim ParamMax As Integer = 999999
-
Select Case iToken
-
Case 1 'constants
-
iIndex = rc.Next(1, UBound(szConstants))
-
Debug.Print(iIndex)
-
szRet = "{" & szConstants(iIndex) & "}"
-
Case 2 'functions
-
iIndex = rc.Next(1, UBound(szFunctions))
-
Debug.Print(iIndex)
-
szRet = szFunctions(iIndex) & "("
-
iParam = rc.Next(1, ParamMax)
-
Debug.Print(iParam)
-
szRet = szRet & iParam & ")"
-
Case 3 'parameters
-
iIndex = rc.Next(1, UBound(szParameters))
-
Debug.Print(iIndex)
-
szRet = "[" & szParameters(iIndex) & "]"
-
Case 4 'values
-
szRet = rc.Next(1, ParamMax)
-
Debug.Print(szRet)
-
End Select
-
Return szRet
-
End Function
-
Private Sub RandomExpressionCreate(ByVal DoNotIncludeBadData As Boolean)
-
'Random object
-
Dim rc As New Random(Date.Now.Millisecond)
-
-
'array of operators
-
Dim szOperators() As String = {"*", "+", "/", "-", "^"}
-
-
'random number for up to 5 operations
-
Dim iOperatorCount As Integer = rc.Next(1, 5)
-
-
'String parts of random math expression
-
Dim szOp1 As String
-
Dim szOp2 As String
-
Dim szOP As String
-
Dim sbExp As New StringBuilder
-
Dim iOp As Integer
-
-
For x As Integer = 1 To iOperatorCount
-
'Operand 1:
-
szOp1 = RandomExpressionCreateOperand()
-
-
'Operator:
-
iOp = rc.Next(1, UBound(szOperators))
-
szOP = szOperators(iOp)
-
-
'Operand 2:
-
szOp2 = RandomExpressionCreateOperand()
-
-
'Write expression
-
sbExp.Append(szOp1 & " " & szOP & " " & szOp2)
-
-
'Add another operator as long as were not at the end
-
If x <> iOperatorCount Then
-
iOp = rc.Next(1, UBound(szOperators))
-
sbExp.Append(" " & szOperators(iOp) & " ")
-
End If
-
Next x
-
-
'Me.txtExpression.Text = sbExp.ToString
-
Debug.Print(sbExp.ToString)
-
End Sub
-
#End Region
-
11 2976
I posted this before but did not get a reply so i'm gonna try it again without diluting with detail.
Real simply put: random numbers are not random even though I seed with time in milliseconds.
Is there some other method to seed or some other concrete randomizer logic that works?
Yep, programs usually only generate pseudo random numbers. If you want real randomness then you'd have to observe some naturally random activities (like movement of atoms). In practice though pseudo random numbers suffice.
If you feel that your original question has been overlooked, please just reply to the original thread. That will bump it to the top of the list again. Please do not make a new post. Please read the Posting Guidelines.
I have merged your threads.
MODERATOR
How fast you request "random" numbers also plays a part.
I noticed that if I requested them too fast I would get duplicates.
And I believe if you do NOT provide a seed, it defaults to using the time anyway.
How fast you request "random" numbers also plays a part.
I noticed that if I requested them too fast I would get duplicates.
And I believe if you do NOT provide a seed, it defaults to using the time anyway.
Yes. I observed this as well. Stepping through the code, random worked. It was being seeded in milliseconds.
What can I use instead of milliseconds? Evidently, my routines are happening too quickly- before the next millisecond.
My temporary solution will be to store used random numbers but only in such a way as not to duplicate more than a few times. I hope this will delay the operation a bit.
If theres a truer way to handle randoms, please let me know.
I dont know what the other poster mean by using Atomic things. Sorry but thanks for the responses, community.
Use a Thread.Sleep() with a one milisecond wait time?
If theres a truer way to handle randoms, please let me know.
I dont know what the other poster mean by using Atomic things. Sorry but thanks for the responses, community.
<That was just the .NET village idiot being himself>
It was just an example of how one could get true randomness. The intent was to show that true randomness is very difficult to get in programs and so pseudo random numbers are an acceptable substitute.
</That was just the .NET village idiot being himself>
Use a Thread.Sleep() with a one milisecond wait time?
Good suggestion. I'll give it a try. Appreciate it
Yes. I observed this as well. Stepping through the code, random worked. It was being seeded in milliseconds.
What can I use instead of milliseconds? Evidently, my routines are happening too quickly- before the next millisecond.
My temporary solution will be to store used random numbers but only in such a way as not to duplicate more than a few times. I hope this will delay the operation a bit.
If theres a truer way to handle randoms, please let me know.
I dont know what the other poster mean by using Atomic things. Sorry but thanks for the responses, community.
Can you not try the DateTime.Ticks property instead? This has a greater resolution that miliseconds...
Can you not try the DateTime.Ticks property instead? This has a greater resolution that miliseconds...
I like that Ticks uses Nanoseconds (just read about it). I'll see if this works. Thanks for the suggestion.
I like that Ticks uses Nanoseconds (just read about it). I'll see if this works. Thanks for the suggestion.
the Ticks count is the default seed if none is specified I believe
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: james blair |
last post by:
Hi
I am generating a random number using
random.randint(1,10000000000)
Whats the possibility that the numbers generated will be same when
generated by 100 users at the same time?
Whats the best...
|
by: lallous |
last post by:
Hello,
This code works fine when 'size' is less than 32768 however when size is
bigger this function never returns.
Can't find out why?!
If I break into the code I can see that 'i' is 32768.......
|
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...
|
by: Anamika |
last post by:
I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.
Two people should not have same numbers....
Also the numbers...
|
by: Xoomer |
last post by:
Hi,
Can some post a code with simple Unique Random Numbers.
Random numbers stored in arr but unique.
|
by: Anil Gupte |
last post by:
I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:
public string GetRand(int count)
{
string number = "";
for (int i=0;...
|
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: 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 ...
|
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
|
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: 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...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |