1. ## Randomize?

Is there any better randomize generator than Visual C++'s own???

Thanx...

rand() ?

random()

VC++'s own?

3. ## Re: Randomize?

Hi there!

Combining rand() with someting else (like the current mouse position and/or the time)?

I prefer using the actual clock speed, because it is REALLY random (because it is a quarz it is changing frequently).

So long,

LG

4. ## Re: Randomize?

How do I write to use the actual ClockSpeed??? Because it's what I've been looking for, if U don't have any good seed the randomize sucks...

5. ## Re: Randomize?

Im pretty sure rand() and random() already use the clock - or something similar to generate their numbers

i find i can get any range of numbers i need with rand()

((rand() % 8000) - 4000) / 1000;

gives me a nice random range of -4.000 to 4.000......

6. ## Re: Randomize?

rand() (dunno about random()) is not using the clock to generate numbers. It's using some algorithm to generate numbers based on a seed. This means you will get the same numbers each time you call rand() with the same seed.
One way to get unique series of numbers is to always call srand(time(NULL)) before each call to rand(). Might be abit slow though. These functions will indirectly use the clock to generate a random number (what igrosshennig mentioned).

7. ## Re: Randomize?

Well, I could talk your ear off about random numbers, but that's not what you're looking for..

rand() as it is implemented on most systems uses a simple division and add with some relatively prime numbers on a seed integer. The seed is updated each time rand() is called. You can generate the same list of numbers using the same seed. This is called Pseudo-Random Number Generation.

If you want a 'stronger' pseudo-random number generator, just type "cryptographic random number generator" into Google and see what pops up. ISSAC is a pretty good one: http://burtleburtle.net/bob/rand/isaacafa.html

If you want TRUE random numbers, ie: you will never get the same series, EVER. You will have to implement some real-world tricks. Minor fluctuations in interrupt speeds, keyboard taps, and wiggles in the mouse actuators are all good sources. However, the best by far is the HotBits server: http://www.fourmilab.ch/hotbits/

You can request up to 2K of random bytes at a time. Then just poll from this set for your random number, or use it as the seed for a pseudo-random number series.

--Bryan

8. ## Re: Randomize?

Hi there!

I am sorry for the delay, but I was busy.
It looks like the thread draws some attention. Well, dosen't matter

As Bob already said rand() uses some bull**** formula to generate (always) the same numbers. As I said the current clockspeed is REALLY random. I present to you the proof. A code snipped to show you how to get it.

Since the windows timegettime() funtcion is limited to milisecond, the results of my funtion are also limited to this. This means the last three digits of the actual clockspeed are missing. BUT you should still get UNIQUE result (i.e. for a 400MHz system you should get values between 380000 and 420000). Well, as I said there a dependend on your system oscilator.

btw. you have to link the winmm.lib to the sample.

--- code snipped of main.cpp ----
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>

DWORD GetClockSpeed()
{
DWORD i;
DWORD StartTimeRDTSC, EndTimeRDTSC;
DWORD StartTimeWIN, EndTimeWIN;
DWORD ProcessorMHZ = 0;
float TimeT = 0.0f;
__asm
{
__emit 0fh // rdtsc
__emit 031h
mov StartTimeRDTSC,eax
}
StartTimeWIN = timeGetTime();
for(i=0; i<1000000; i++) TimeT += ((float)(rand()%19)/2.0f*(float)i);

__asm
{
__emit 0fh // rdtsc
__emit 031h
mov EndTimeRDTSC,eax
}

EndTimeWIN = timeGetTime();
ProcessorMHZ = (EndTimeRDTSC-StartTimeRDTSC)/((EndTimeWIN-StartTimeWIN));
return ProcessorMHZ;
};

void main(void)
{
for(int i=0; i < 20; i++)
{
cout << GetClockSpeed() << "\n";
}
// wait till the user enters something
char something;
cin >> something;
};

--- code snipped stops----

Hm, it is probably in a bad format now (due to the post client...), but you will figure
it out .

Don't get nervous about the rand() inside the function, it is only there to keep the cpu busy

I you have still questions simply ask me

Have fun, and may the vector be with you!

LG

9. ## Re: Randomize?

Thanks, I'll try that...

10. ## Re: Randomize?

Sorry LG, this code is NOT random. The actual 'randomness' from your algorithm is again coming from the rand() function.

Let me explain:
When the processor executes an instruction, the cycle counter increments based on the cycles it took to execute that instruction. This is great for simple stuff like Shift, Move, and memory accesses because these instructions execute in exactly the same number of cycles regardless.

When you throw in Mod, Mult, Div, etc.. the number of cycles becomes a bit less static. Depending on the number of bits that are set in the mantissa of the numbers, the Div operation takes differing amounts of time.

It is this differing amount of time in the Div and Mod operations of your loop that is generating the apparently random numbers you see, not clock cycle fluctuation.

This is much worse than rand() in that the randomness of rand() is mathematicaly bound and known. Allowing you to work within a known level of error and make compensations for it. All computers will generate the same error within these bounds.

With your function, the bounds of the randomness are not well defined: on my overclocked AMD K6-2 450, I would get pretty good results but on a very stable Intel PIII 1GHz, your randomness would be almost exactly the same if you reset the randseed() each time (because the same computations would be performed, thus causing the same cycles to be used, thus the same resulting clock speed).

Here's an algorithm for capturing random real-time interrupt fluctuation:
Trap the real time interrupt to occur at the shortest allowable time.
Start a VERY optimized counter routine at zero.
When the interrupt is called, store your counter.
Do this again to get two counts.
If the first count is less, return one bit = 1. If the first count is more, return one bit = 0. If they were equal, do not return any bits.

After producing 8 bits, you have one random byte. Repeat for all the bytes you need.

I've used this system on three platforms and tested the results with the random-number generator monkey tests. All the streams passed the tests. This still isn't good enough for me, but it works in a pinch.

Hope that helps,
--Bryan

Page 1 of 3 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•