|
-
May 31st, 2010, 06:15 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Uptimize my Random Number Generator
Hello guys,
Am using a random number generator in a loop
I used this function to generate random number in vb.net and it worked fine
vb Code:
'//Generate RandomNumber
Private Function RandomNum(ByVal Lower As Long, _
ByVal Upper As Long) As Long
Randomize(Timer)
RandomNum = Rnd() * (Upper - Lower + 1) + Lower
End Function
Converting to c# made is extremely slow
c# Code:
////Generate RandomNumber
private int RandomNum(int Lower, int Upper)
{
System.Random RandNum = new System.Random();
int MyRandomNumber = RandNum.Next(Lower, Upper);
System.Threading.Thread.Sleep(100);// this line makes the random generator give unique value
return MyRandomNumber;
}
Please can you help me uptimize it?
-
May 31st, 2010, 06:36 AM
#2
Re: Uptimize my Random Number Generator
Dont create a new Random object each time the function is called.
-
May 31st, 2010, 06:58 AM
#3
Thread Starter
Fanatic Member
Re: Uptimize my Random Number Generator
Hello, thanks, so how do i do that.
perhaps you could give me some code.
Last edited by coolcurrent4u; May 31st, 2010 at 07:09 AM.
Reason: typo
-
May 31st, 2010, 07:22 AM
#4
Re: Uptimize my Random Number Generator
Also, why do you need Sleep in there? 
Code:
public partial class Form1 : Form
{
System.Random RandNum = new System.Random();
private int RandomNum(int Lower, int Upper)
{
int MyRandomNumber = RandNum.Next(Lower, Upper);
return MyRandomNumber;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = RandomNum(1, 100).ToString();
}
}
-
May 31st, 2010, 08:01 AM
#5
Re: Uptimize my Random Number Generator
The reason he was using Sleep is that the random function uses the current time as a seed, and thus calling Next too fast will return identical values.
This is all avoided by using one random instance only, as it'll always return an unique value even though its Next function is called in a tight loop.
-
May 31st, 2010, 08:08 AM
#6
Frenzied Member
Re: Uptimize my Random Number Generator
hey,
Code:
System.Threading.Thread.Sleep(100);// this line makes the random generator give unique value
who said this will generate a unique value?
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
May 31st, 2010, 10:27 AM
#7
Re: Uptimize my Random Number Generator
 Originally Posted by Atheist
The reason he was using Sleep is that the random function uses the current time as a seed, and thus calling Next too fast will return identical values...
Never saw it happened before, although I don't use random numbers often.
-
May 31st, 2010, 11:49 AM
#8
Thread Starter
Fanatic Member
Re: Uptimize my Random Number Generator
 Originally Posted by RhinoBull
Also, why do you need Sleep in there?
Code:
public partial class Form1 : Form
{
System.Random RandNum = new System.Random();
private int RandomNum(int Lower, int Upper)
{
int MyRandomNumber = RandNum.Next(Lower, Upper);
return MyRandomNumber;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = RandomNum(1, 100).ToString();
}
}
This gives identical values in a for loop. That is why i was using that line
c# Code:
System.Threading.Thread.Sleep(100);
And can i place two class in one file?
i had to place you solution in a form to test it
-
May 31st, 2010, 12:06 PM
#9
Thread Starter
Fanatic Member
Re: Uptimize my Random Number Generator
I used a tool online to convert my vb.net function to c#
i got this
c# Code:
private long RandomNum(long Lower, long Upper)
{
VBMath.Randomize(Timer);
return VBMath.Rnd() * (Upper - Lower + 1) + Lower;
}
c# can't find these functions VBMath.Randomize,Timer,VBMath.Rnd
any help
-
May 31st, 2010, 03:56 PM
#10
Thread Starter
Fanatic Member
Re: Uptimize my Random Number Generator
ok Guys i found the solution
At the top of my class where i place class member variable i do this
c# Code:
private static Random rnd = new Random(Environment.TickCount);
And my new function
c# Code:
//Generate RandomNumber
private int RandomNum(int Lower, int Upper)
{
return rnd.Next(Lower, Upper);
}
Thanks for all you support
-
May 31st, 2010, 05:54 PM
#11
Re: [RESOLVED] Uptimize my Random Number Generator
Well, in a nutshell thats just what I wrote in my first post.
-
May 31st, 2010, 11:44 PM
#12
Frenzied Member
Re: [RESOLVED] Uptimize my Random Number Generator
great method with out thread.sleep
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jun 1st, 2010, 03:44 AM
#13
Re: [RESOLVED] Uptimize my Random Number Generator
Just a little clarification. When you create a new Random object, it must use a seed value to determine its sequence of random numbers. If no seed value is given, the system time is used (which is sufficient in most cases).
If you create multiple Random objects all with the same seed value, then they will generate the exact same sequence of "random" numbers. This is also why these random numbers are called "pseudo-random". They aren't absolutely random, but they are generated using some formula and the seed value. If you know the formula and the seed value, you can generate the exact same sequence of numbers.
So, the Random object doesn't just generate a single value, it can generate a whole sequence of values. You get the next number in the sequence using the Next method.
If you create new Random object every time you need a random number, and then call that method multiple times in a loop, then chances are that your loop will run so fast that the system time (= seed value for ALL random objects created) will be the same for at least 4 or 5 iterations. Hence, these Random objects all generate the same sequence of numbers, and because you are only using the very first every time, you will see that they generate the same "random" numbers.
Adding a short sleep to the loop will make it work, because the seed value (system time) is now no longer the same for two iterations. The sequence of random numbers (of which you are still using only the first) is different each time, and you get different numbers. However, it is not a very good solution, because as you have noticed, adding a sleep of 0.1 second every iteration will slow down your loop a LOT. A mere 10 iterations will make it take a whole second, where 10 iterations usually run in a few microseconds. That's a factor 1.000.000 slower, for absolutely no reason.
-
Jun 1st, 2010, 04:02 AM
#14
Thread Starter
Fanatic Member
Re: [RESOLVED] Uptimize my Random Number Generator
 Originally Posted by NickThissen
Just a little clarification. When you create a new Random object, it must use a seed value to determine its sequence of random numbers. If no seed value is given, the system time is used (which is sufficient in most cases).
If you create multiple Random objects all with the same seed value, then they will generate the exact same sequence of "random" numbers. This is also why these random numbers are called "pseudo-random". They aren't absolutely random, but they are generated using some formula and the seed value. If you know the formula and the seed value, you can generate the exact same sequence of numbers.
So, the Random object doesn't just generate a single value, it can generate a whole sequence of values. You get the next number in the sequence using the Next method.
If you create new Random object every time you need a random number, and then call that method multiple times in a loop, then chances are that your loop will run so fast that the system time (= seed value for ALL random objects created) will be the same for at least 4 or 5 iterations. Hence, these Random objects all generate the same sequence of numbers, and because you are only using the very first every time, you will see that they generate the same "random" numbers.
Adding a short sleep to the loop will make it work, because the seed value (system time) is now no longer the same for two iterations. The sequence of random numbers (of which you are still using only the first) is different each time, and you get different numbers. However, it is not a very good solution, because as you have noticed, adding a sleep of 0.1 second every iteration will slow down your loop a LOT. A mere 10 iterations will make it take a whole second, where 10 iterations usually run in a few microseconds. That's a factor 1.000.000 slower, for absolutely no reason.
Thanks for the info.
I know adding sleep makes the random number unique, but i can sacrifice the time. Note that i generate each seed each time i call the class.
IMO i think this would make each set of random numbers as unique as possible. You could run a test to prove it wrong or something.
I onces red in a math book that there is no way of generating true random number using a computer .
So the environment/programming language is using some sort of formula as you said.
Am open to suggestion and correction.
Note that i use the word "Optimize" in the thread name.
-
Jun 1st, 2010, 04:17 AM
#15
Re: [RESOLVED] Uptimize my Random Number Generator
First of all, the term "unique" is not really appropriate. Unique usually means that you want to generate a set of random numbers without repeating any numbers (so for example 41802 is a set of 5 unique numbers, while 41804 is not). Adding a sleep will not make your random number generators generate unique random numbers.
Now, you say that you can sacrifice the time, yet in the very first post (presumably the reason you made this post in the first place) you say that your code has suddenly slowed down. Of course it has, you are doing nothing for 0.1 second every iteration!
Whether using a single Random object or using multiple Random objects with different seed values create "more random" numbers is hard to determine. A true random number generator generates each number with equal probability, but even after generating millions of numbers you'll always see a few numbers that have been chosen more times, simply due to chance.
You cannot generate a true random number using computer algorithms, no, but you can (well, this is up for debate really) using a computer. For example, you could use the mouse movements to determine a random number, or you could measure the temperature of your CPU very accurately and use that, or ... You'd have to use something that is outside of anyone's control.
Of course, you can argue that even the mouse movements someone makes aren't random, as he is actively controlling the mouse, but at least there is no way to predict those movements, so you cannot predict the random numbers either. If you know the formula and seed value for any random number generator using an algorithm, you can predict all the numbers it will spit out simply by applying the formula.
-
Jun 2nd, 2010, 02:33 AM
#16
Thread Starter
Fanatic Member
Re: [RESOLVED] Uptimize my Random Number Generator
NickThissen what you said is true, to some extent!
My app generates random number in sets, eg like 10, 20 etc, so it is crucial that the random generator generation of numbers is fast and random.
There is a problem with this random generation though. if for example i pass 1 as min and 10 as max, and i loop 10 times. I'd expect that no number repeats itself, bu this is not true some numbers are never used while other repeat at least once or never.
In an application where it is crucial that no number repeat, it then means i'll use an alternative way. This is the algorithm.
Code:
Generate numbers from min to max, and sore in array
Shuffle the array
pick numbers from the lowest value to the largest
loop until exhausted
This will ensure that at least each number is used once, only unique is some cases not all. In a situation where only a single number is required, the this system will fail.
Am still working on some algorithm, and will post it here when i finish.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|