Generatin Random Numbers

This is a discussion on Generatin Random Numbers within the C Programming forums, part of the General Programming Boards category; hi guys any idea as to how to generate random numbers using pointers instead of the using function random.....without using ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    21

    Generatin Random Numbers

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    21
    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????

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can get random numbers from the Internet, for example http://www.random.org/
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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"

  7. #7
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    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?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    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;
    }
    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.

    --
    Mats

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    ...came to the conclusion that his randomly flashing christmas lights worked best if he put a code-prom in the "random number" prom.
    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"... :-)

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rpet View Post
    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"... :-)
    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

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by matsp View Post
    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.

    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 = &#37;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;
    }
    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.

    --
    Mats
    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

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by maxorator View Post
    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.
    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"

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    21
    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....

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by iMalc View Post
    Actually, 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.
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21