16,017,881 members

C#

private void BarcodeButton_Click(object sender, EventArgs e) { try { int smallestNumber = 100000; int biggestNumber = 5000000; //Determine the amount of random numbers int amountOfRandomNumbers = 10; //Create a list of numbers from which the routine //shall choose the result numbers var possibleNumbers = new List<int>(); for (int i = smallestNumber; i <= biggestNumber; i++) possibleNumbers.Add(i); var result = new List<int>(); //Initialize a random number generator Random rand = new Random(); //For-loop which picks each round a unique random number for (int i = 0; i < amountOfRandomNumbers; i++) { //Generate random number int randomNumber = rand.Next(1, possibleNumbers.Count) - 1; barcodeTextEdit.Text = randomNumber.ToString(); //Remove the chosen result number from possible numbers list possibleNumbers.RemoveAt(randomNumber); }

this code true random number or pseudo random generator and why please ?

pseudo random - there's a great explanation here C# in Depth: Random numbers[^]

Permalink

Share this answer

Comments

andrew nader
27-May-16 9:29am

what is seed in this code please ??

Garth J Lancaster
27-May-16 21:42pm

see OriginalGriff's answer - seed 'could be' anything - it could be (as even I remember doing) a time value, or based on Pi as per OG's answer ... the only way of getting around the limitations of 'seed' as such is to increase the 'entropy' - which is another discussion - see Thomas Pornin's answer here http://stackoverflow.com/questions/3436376/what-is-the-most-secure-seed-for-random-number-generation

andrew nader
31-May-16 7:50am

class rand in c# use Midsquare method , Linear Congruential Method,Combined Linear Congruential Generators or Random-Number Streams and why

All software based methods are pseudo random: They use a "fixed algorithm" to generate the sequence of "random" numbers and the whole sequence can be repeated by providing the same seed value. The Random class uses the system clock to initialize the sequence when the instance is created, as can easily be seen if two instances are created in quick succession.

Permalink

Share this answer

Comments

andrew nader
29-Apr-16 6:53am

what is fixed algorithm ?

OriginalGriff
29-Apr-16 7:00am

What do you think it is?

andrew nader
29-Apr-16 7:03am

how to check random in this code?

OriginalGriff
29-Apr-16 7:07am

https://en.wikipedia.org/wiki/Randomness_tests

andrew nader
27-May-16 9:50am

what is seed in this code please??

OriginalGriff
27-May-16 10:12am

"random number" generators don't generate truly random numbers: they can't because the algorithms are necessarily deterministic. So they use a "seed" value to provide a "starting point" for the algorithm.

For example, if you want random integers between 0 and 9, you could use teh digits of Pi and might use a seed value that selected a number of digits after the decimal point to start. So given a value of pi of

3.141592653589793238462643383279502884197169399375105820974944592307816406286...

you would get a different sequence of digits if you start with a seed of 12:

9

7

9

3

2

3

That you would get if you started with a seed of 23:

4

3

3

8

3

2

But each time you use the same seed value, you will get exactly the same sequence of digits.

Random number algorithms don't use the digits of Pi, but they still generate the same sequence for the same seed.

In the case on .NET (and most other systems) the seed value is taken from the value of the system clock when the Random class instance is created. You never get to see that, because you don't need to know it - but that's why you create a Random instance as a static object, or at class level rather than generating one every time you need a random number: processors are quick enough that the new sequence can be the same as the last because the system clock has not changed in the meantime.

For example, if you want random integers between 0 and 9, you could use teh digits of Pi and might use a seed value that selected a number of digits after the decimal point to start. So given a value of pi of

3.141592653589793238462643383279502884197169399375105820974944592307816406286...

you would get a different sequence of digits if you start with a seed of 12:

9

7

9

3

2

3

That you would get if you started with a seed of 23:

4

3

3

8

3

2

But each time you use the same seed value, you will get exactly the same sequence of digits.

Random number algorithms don't use the digits of Pi, but they still generate the same sequence for the same seed.

In the case on .NET (and most other systems) the seed value is taken from the value of the system clock when the Random class instance is created. You never get to see that, because you don't need to know it - but that's why you create a Random instance as a static object, or at class level rather than generating one every time you need a random number: processors are quick enough that the new sequence can be the same as the last because the system clock has not changed in the meantime.

andrew nader
27-May-16 10:37am

int amountOfRandomNumbers = 10;

var possibleNumbers = new List<int>();

for (int i = smallestNumber; i <= biggestNumber; i++)

possibleNumbers.Add(i);

What is meant by this.??

int randomNumber = rand.Next(1, possibleNumbers.Count) - 1;

and what is meant by -1 ???

thanks alot

var possibleNumbers = new List<int>();

for (int i = smallestNumber; i <= biggestNumber; i++)

possibleNumbers.Add(i);

What is meant by this.??

int randomNumber = rand.Next(1, possibleNumbers.Count) - 1;

and what is meant by -1 ???

thanks alot

OriginalGriff
27-May-16 10:45am

"subtract one" - what did you think it meant? :laugh:

andrew nader
27-May-16 10:49am

i know subtract one.but i means what is benefit of subtract one ?? :laugh:

OriginalGriff
27-May-16 10:58am

It offsets a number range of (say) 1-10 to 0-9.

Silly: he could have done the same thing with:

rand.Next(0, possibleNumbers.Count - 1)

Since the lower bound is inclusive.

But what he probably really wanted was:

rand.Next(0, possibleNumbers.Count)

Since the upper bound is exclusive, so if possibleNumbers.Count is 10, the range of output values will be 0-9 inclusive.

Silly: he could have done the same thing with:

rand.Next(0, possibleNumbers.Count - 1)

Since the lower bound is inclusive.

But what he probably really wanted was:

rand.Next(0, possibleNumbers.Count)

Since the upper bound is exclusive, so if possibleNumbers.Count is 10, the range of output values will be 0-9 inclusive.

andrew nader
31-May-16 7:50am

class rand in c# use Midsquare method , Linear Congruential Method,Combined Linear Congruential Generators or Random-Number Streams and why

OriginalGriff
31-May-16 8:52am

Look at the Reference Sources and they may tell you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900