473,326 Members | 2,173 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,326 software developers and data experts.

random number question

Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100),
"0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks
Nov 16 '05 #1
6 4665
Hi,

I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();

Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100),
"0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks

Nov 16 '05 #2
Hi Peter

That looks like it might suit our needs, Thanks

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
Hi,

I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();

Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer *
100), "0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks


Nov 16 '05 #3
I use the following... Gives Better results...

using System;

using System.Security.Cryptography; // Needed for a descent random number
generator.

namespace Utilities

{

// This random generator uses the crypto service provider because the normal
random functions in .NET

// have repeating number strings and other such problems.

public class RandomNumberGenerator

{

byte[] randbyte = new byte[0];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
public int getRandomNumber(int min, int max)

{

rng.GetNonZeroBytes(randbyte);

Random rand = new Random(Convert.ToInt32(randbyte[0]));

return rand.Next(min, max);

}

}

}

"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OS****************@TK2MSFTNGP14.phx.gbl...
Hi Peter

That looks like it might suit our needs, Thanks

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
Hi,

I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();

Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer *
100), "0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks



Nov 16 '05 #4
I still don't see a point why would you always create a new instance of the
Random class. Once you create it, it's supposed to return different values
for the next 2^32 calls of the Next method.
If you do new Random(DateTime.Now.Milliseconds).Next(), your "random" number
changes only with the frequency of DateTime.Now changes (16 ms for me), and
it repeats every second!!!

a simple:

static Random rnd = new Random();

and using rnd.Next() anytime you use it should be all that you need...

HTH,
Stefan

"Glenn Wilson" <ir****@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP15.phx.gbl...
I use the following... Gives Better results...

using System;

using System.Security.Cryptography; // Needed for a descent random number
generator.

namespace Utilities

{

// This random generator uses the crypto service provider because the
normal random functions in .NET

// have repeating number strings and other such problems.

public class RandomNumberGenerator

{

byte[] randbyte = new byte[0];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
public int getRandomNumber(int min, int max)

{

rng.GetNonZeroBytes(randbyte);

Random rand = new Random(Convert.ToInt32(randbyte[0]));

return rand.Next(min, max);

}

}

}

"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OS****************@TK2MSFTNGP14.phx.gbl...
Hi Peter

That looks like it might suit our needs, Thanks

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
Hi,

I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();

Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer *
100), "0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks



Nov 16 '05 #5
And as for the example with RNGCryptoServiceProvider, it can't even work and
isn't reentrant.

byte[] randbyte = new byte[0]; ????

public int getRandomNumber(int min, int max)

{

rng.GetNonZeroBytes(randbyte);

// will always throw IndexOutOfRangeException
Random rand = new Random(Convert.ToInt32(randbyte[0]));

return rand.Next(min, max);

}

Even if there was one byte allocated, the generator would always be able to
generate only 255 random values (though in random order).

It could possibly work like this, though:

public class RandomNumberGenerator
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

public int getRandomNumber(int min, int max)
{
byte[] rand = new byte[4];
rng.GetBytes(rand);

int num = BitConverter.ToInt32(rand, 0);

return min + num % (max - min);
}
}

But I'm sure that the basic Random() class should be more than sufficent for
any non-cryptographical use.

HTH,
Stefan

"Glenn Wilson" <ir****@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP15.phx.gbl...
I use the following... Gives Better results...

using System;

using System.Security.Cryptography; // Needed for a descent random number
generator.

namespace Utilities

{

// This random generator uses the crypto service provider because the
normal random functions in .NET

// have repeating number strings and other such problems.

public class RandomNumberGenerator

{

byte[] randbyte = new byte[0];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
public int getRandomNumber(int min, int max)

{

rng.GetNonZeroBytes(randbyte);

Random rand = new Random(Convert.ToInt32(randbyte[0]));

return rand.Next(min, max);

}

}

}

"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OS****************@TK2MSFTNGP14.phx.gbl...
Hi Peter

That looks like it might suit our needs, Thanks

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
Hi,

I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();

Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer *
100), "0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks



Nov 16 '05 #6
Thanks Glenn

"Glenn Wilson" <ir****@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP15.phx.gbl...
I use the following... Gives Better results...

using System;

using System.Security.Cryptography; // Needed for a descent random number
generator.

namespace Utilities

{

// This random generator uses the crypto service provider because the
normal random functions in .NET

// have repeating number strings and other such problems.

public class RandomNumberGenerator

{

byte[] randbyte = new byte[0];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
public int getRandomNumber(int min, int max)

{

rng.GetNonZeroBytes(randbyte);

Random rand = new Random(Convert.ToInt32(randbyte[0]));

return rand.Next(min, max);

}

}

}

"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OS****************@TK2MSFTNGP14.phx.gbl...
Hi Peter

That looks like it might suit our needs, Thanks

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
Hi,

I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();

Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi

In VB6 we used the following to create a unique random number -

Function longSerial() As Long

longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer *
100), "0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)

End Function

Seem to be unable to do similar in c#, can anyone assist

Thanks



Nov 16 '05 #7

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

Similar topics

3
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which...
6
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I've got two random number/statistics questions I'd like you to review. My first question is not directly related to PHP, but will be implemented in PHP, as explained in my second...
23
by: MConly | last post by:
Can you tell me what happens inside CPU when I rand() ? Where can I find the true rand function implemented ? I have heard that rand() in C/C++ is n't a good one but why it isn't a good one, are...
4
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
11
TTCEric
by: TTCEric | last post by:
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.