hi guys
any idea as to how to generate random numbers using pointers instead of the using function random.....without using random function how to generate random numbers......
any ideas???
thanks
regards
harsha
hi guys
any idea as to how to generate random numbers using pointers instead of the using function random.....without using random function how to generate random numbers......
any ideas???
thanks
regards
harsha
Write your own random function if you do not want to (or somehow cannot) use srand() and rand(). Take a look at Prelude's Random Numbers Tutorial.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
no i dont want to use any function at all....just using pointers i wanna print 5 random numbers b/w 1 to 100....
is it possible without using any type of function????
Maybe connect your brains to the computer?
Of course it's not possible. Do you expect it to think as a human or something?
"The Internet treats censorship as damage and routes around it." - John Gilmore
You can get random numbers from the Internet, for example http://www.random.org/
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
dantu1985: You absolutely must explain your reasoning for not wanting to use rand!
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Ya, explain why you don't want to use them, seems stupid that you don't, do you not know about seeding the random num gen with the time?
Ok, here's an idea:
Make up a pointer, grab the content of this pointer as a byte, do modulo 100 and add 1. Increment pointer. This will be pretty random if the memory contains for example code. However, the difficult bit will be to generate a pointer to some code, and knowing where to stop.
Of course, just like any other "static" RNG, it will give the same sequence of random numbers each time you run it - at least untile the code changes. And it's very dependant on the code available - for example, the test I wrote, gives an average over 100 of 17.7, where you'd expect 50 or thereabouts. (This is because the number zero is more common in code than other numbers). I made some mods to the below code to avoid repeated patterns, and the average went to 43 or so - which isn't that bad. [It went to 19 by just starting at "printf" instead of at the beginning of the code section (0x400000)]
Here's an example I just hacked up.
By the way, this is NOT good programming style, nor do I recommend this method of generating random numbers - it's actually very BAD way to get random numbers - although I did get the idea from a former manager that after some time of generating random numbers by hand or by "math" came to the conclusion that his randomly flashing christmas lights worked best if he put a code-prom in the "random number" prom.Code:// rng.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #define NR 100 unsigned char *p = (unsigned char *)0x400000; unsigned int rng(void) { unsigned int r; r = *p; printf("*p = %02x\n", r); p += 1; r %= 100; r++; return r; } int _tmain(int argc, _TCHAR* argv[]) { unsigned int r; unsigned int s = 0; int i; for(i = 0; i < NR; i++) { r = rng(); s += r; printf("r = %d\n", r); } printf("%f\n", (float)s / NR); return 0; }
--
Mats
And did he put the random number PROM in the free code-PROM-slot? I bet that would have made the flashing christmas lights "extra random"... :-)...came to the conclusion that his randomly flashing christmas lights worked best if he put a code-prom in the "random number" prom.
There was no actual processor, just a small PGA that walked across a PROM for the random numbers (at least that was how I understood it, I never had anything to do with the project - I just heard about it several years after he did it, and he was more of a hardware than software person anyways).
--
Mats
Or you could get paging file size and do modulo 100, +1, on that. I don't know how you'd get it but it should be pretty random.
It is actually not the code you are making those numbers of, 0x400000-0x401000 is the PE header (as a kernel hacker you should know that ). Why not set p point to your main function?
Anyway, we can all agree that without using any external functions it is not possible to create random numbers, which are always different when you run the program.
"The Internet treats censorship as damage and routes around it." - John Gilmore
Actaully, we can't quite agree on that. I'm familiar with an architecture that can generate non-pseudo random numbers by measuring the randomly varying voltage of a certain part of the internal circuitry (VIC-20).
Also, it is not hard to write the code for a Linear congruential pseudo random number generator directly into where you need it, thus not actually requiring a seperately called function.
The fact of the matter is though, that there is no point in specifically avoiding calling a function to produce a random number. There's also little point suggesting things until we know the reasoning behind this, either. If he want's cryprographically secure random numbers, then there are things available for that.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
hey thanks a lot....will try tat out....i dont want to use a function cause me and friend were just tryin different stuff.....like a small project or something.,.....tats y....
I was talking about PCs... and I don't think it is possible to generate a random without calling a function because for seeding it with the time you need to call specific functions to get the time (unless it's a VERY low-level driver).
But the point is actually simple yes - avoiding using functions isn't a good idea.
"The Internet treats censorship as damage and routes around it." - John Gilmore