473,406 Members | 2,467 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.

Getting random, unique numbers

TTCEric
26
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:

Expand|Select|Wrap|Line Numbers
  1. #Region "Random Number Generator does not work"
  2.     Private Function RandomExpressionCreateOperand() As String
  3.         'Array of constants 
  4.         Dim szConstants() As String = {"pi", "e"}
  5.         'Array of functions 
  6.         Dim szFunctions() As String = {"Acos", "Asin", "Atan", "Ceiling", "Cos", "Cosh", "Floor", "Log", "Sin", "Sqrt", "Tan"}
  7.         'Array of parameters (proprietary)
  8.         Dim szParameters() As String = {"Parameter1", "Param.eu"}
  9.         'Array of tokens (corresponding to Constant, Fucntion, Parameter or raw value, respectively)
  10.         Dim iTokens() As Integer = {1, 2, 3, 4}
  11.         'Seed random number generator
  12.         Dim rc As New Random(Date.Now.Millisecond)
  13.         'Random number related to token (Constant, Function, Parameter or Value)
  14.         Dim iToken As Integer = rc.Next(1, UBound(iTokens))
  15.         'Return string 
  16.         Dim szRet As String = ""
  17.         'Index of value in the array
  18.         Dim iIndex As Integer = 0
  19.         'Raw value but also parameter of a function token
  20.         Dim iParam As Integer = 0
  21.         'Max value of parameter or raw value: 
  22.         Dim ParamMax As Integer = 999999
  23.         Select Case iToken
  24.             Case 1 'constants
  25.                 iIndex = rc.Next(1, UBound(szConstants))
  26.                 Debug.Print(iIndex)
  27.                 szRet = "{" & szConstants(iIndex) & "}"
  28.             Case 2 'functions 
  29.                 iIndex = rc.Next(1, UBound(szFunctions))
  30.                 Debug.Print(iIndex)
  31.                 szRet = szFunctions(iIndex) & "("
  32.                 iParam = rc.Next(1, ParamMax)
  33.                 Debug.Print(iParam)
  34.                 szRet = szRet & iParam & ")"
  35.             Case 3 'parameters
  36.                 iIndex = rc.Next(1, UBound(szParameters))
  37.                 Debug.Print(iIndex)
  38.                 szRet = "[" & szParameters(iIndex) & "]"
  39.             Case 4 'values
  40.                 szRet = rc.Next(1, ParamMax)
  41.                 Debug.Print(szRet)
  42.         End Select
  43.         Return szRet
  44.     End Function
  45.     Private Sub RandomExpressionCreate(ByVal DoNotIncludeBadData As Boolean)
  46.         'Random object
  47.         Dim rc As New Random(Date.Now.Millisecond)
  48.  
  49.         'array of operators
  50.         Dim szOperators() As String = {"*", "+", "/", "-", "^"}
  51.  
  52.         'random number for up to 5 operations
  53.         Dim iOperatorCount As Integer = rc.Next(1, 5)
  54.  
  55.         'String parts of random math expression
  56.         Dim szOp1 As String
  57.         Dim szOp2 As String
  58.         Dim szOP As String
  59.         Dim sbExp As New StringBuilder
  60.         Dim iOp As Integer
  61.  
  62.         For x As Integer = 1 To iOperatorCount
  63.             'Operand 1: 
  64.             szOp1 = RandomExpressionCreateOperand()
  65.  
  66.             'Operator:  
  67.             iOp = rc.Next(1, UBound(szOperators))
  68.             szOP = szOperators(iOp)
  69.  
  70.             'Operand 2: 
  71.             szOp2 = RandomExpressionCreateOperand()
  72.  
  73.             'Write expression 
  74.             sbExp.Append(szOp1 & " " & szOP & " " & szOp2)
  75.  
  76.             'Add another operator as long as were not at the end
  77.             If x <> iOperatorCount Then
  78.                 iOp = rc.Next(1, UBound(szOperators))
  79.                 sbExp.Append(" " & szOperators(iOp) & " ")
  80.             End If
  81.         Next x
  82.  
  83.         'Me.txtExpression.Text = sbExp.ToString
  84.         Debug.Print(sbExp.ToString)
  85.     End Sub
  86. #End Region
  87.  
Jul 25 '08 #1
11 2976
TTCEric
26
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?
Jul 28 '08 #2
r035198x
13,262 8TB
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.
Jul 28 '08 #3
Curtis Rutland
3,256 Expert 2GB
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
Jul 28 '08 #4
Plater
7,872 Expert 4TB
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.
Jul 28 '08 #5
TTCEric
26
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.
Jul 28 '08 #6
Plater
7,872 Expert 4TB
Use a Thread.Sleep() with a one milisecond wait time?
Jul 28 '08 #7
r035198x
13,262 8TB
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>
Jul 29 '08 #8
TTCEric
26
Use a Thread.Sleep() with a one milisecond wait time?
Good suggestion. I'll give it a try. Appreciate it
Jul 29 '08 #9
IanWright
179 100+
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...
Jul 29 '08 #10
TTCEric
26
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.
Jul 29 '08 #11
Plater
7,872 Expert 4TB
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
Jul 29 '08 #12

Sign in to post your reply or Sign up for a free account.

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...
4
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...
5
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.......
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...
6
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...
17
by: Xoomer | last post by:
Hi, Can some post a code with simple Unique Random Numbers. Random numbers stored in arr but unique.
8
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;...
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: 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
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,...
0
isladogs
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...

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.