Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

System.Random(Int32) help

I'm having trouble understanding exactly how the seed works in System.Random(Int32).

Say I have the following code:

Dim random As Random = New Random(620757230)
intNext = random.Next(0, 3)

This is saying give me a random number between 0 and 2, right?  What exactly is the seed doing for this random number generator?
0
nightshadz
Asked:
nightshadz
  • 5
  • 3
1 Solution
 
käµfm³d 👽Commented:
What exactly is the seed doing for this random number generator?
If don't know, "random" numbers aren't truly random when talking about a computer. Such numbers are generated by some algorithm. This algorithm has a default "seed" (I don't know what it is for .NET). If you provide the same seed to two different Random objects, you should get the same value for each successive call to Next() on the objects. Providing different seeds changes the values you receive on subsequent calls to Next().
0
 
käµfm³d 👽Commented:
If you provide the same seed to two different Random objects, you should get the same value for each successive call to Next() on the objects.
This is dependent on the algorithm, however. There is the possibility that the algorithm used by the Random class combines the seed value with some internal, volatile value, like system time. In that case, you might not receive the same value from each Random object.
0
 
nightshadzAuthor Commented:
You have the option of not providing a seed. In my example I'm providing a seed but getting numbers between 0 and 2. How does providing a seed provide better randomness in this example. Assume the seed changes every time.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
käµfm³d 👽Commented:
I believe when you call Next(), what's happening under the hood is that a (real) number between 0 and 1 is being generated. When you call the Next() function with bounds, that generated number is multiplied (and rounded in the case of int's) to give you a value between you bounds.

How does providing a seed provide better randomness in this example.
I don't think it really changes the "randomness" of the number you receive; rather it changes the sequence of numbers you receive. The numbers only *appear* random to you because you don't know the algorithm being used. If you did, you could "predict" the next number on subequent Next() calls. "Random" numbers are called pseudo-random because of their appearance of being random.
0
 
käµfm³d 👽Commented:
Say for example you created a default Random (no seed) object. Let's say it generated the following sequence of numbers for five successive calls:

7893, 9783, 897234, 89322, 2348

Open in new window


Passing in a seed of say 105 might produce this list instead:

9834, 9378, 2378, 833, 8284

Open in new window


Pssing in a see of 837 might produce this list:

92734, 8373489, 926, 9723471, 8383

Open in new window


Passing in a see of 105 again would produce the same list as before:

9834, 9378, 2378, 833, 8284

Open in new window


So a different seed produces a different list.
0
 
nightshadzAuthor Commented:
Maybe I'm not asking my question correctly. When you provide different seed values but use random.next(0,3) what is happening here? How does the result differ with different seed values?
0
 
käµfm³d 👽Commented:
Maybe I'm not asking my question correctly. When you provide different seed values but use random.next(0,3) what is happening here? How does the result differ with different seed values?
Combine everything I said above, primarily the first part of http:#35703185 .

You're using a small domain, so I don't think you'll notice that much of a "randomness". Expand the domain and then start playing with the seed. You should see more of a difference in the range.
0
 
nightshadzAuthor Commented:
Thanks, I tested different seeds and got different lists like you said.  I understand better how this works now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now