Create my own rand function?

This is a discussion on Create my own rand function? within the C++ Programming forums, part of the General Programming Boards category; There is one question in my mind. What's the logic behind rand function. That is how can we write our ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Create my own rand function?

    There is one question in my mind. What's the logic behind rand function. That is how can we write our own rand function.

    One approach may be to use garbage value of variables
    Code:
    int main()
    {
    int a[10];
    for(int i=0;i<=10;++i)
      cout<<a[i];
    }
    But problem with this code is that it will always print same numbers. Is garbage value a fixed value at a memory location. That is say at memory location X if not initialised i will get same value each time. What can be reason behind this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > One approach may be to use garbage value of variables
    1. even reading undefined values may cause exceptions
    2. you run off the end of the array

    Common implementations of rand() use this
    http://www.physics.orst.edu/~rubin/n...mc3/node2.html
    Such algorithms are only any good for student exercises.

    For statistical quality rand functions, consider this one
    http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

    For cryptographic quality rand functions, you really need an entropy pool based approach, like
    http://publib.boulder.ibm.com/infoce...les/random.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    I suggest that you read what Prelude has to say about Random Numbers.
    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

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    It is very surpising and shoking to see that there is same garbage value at a position in main memory. No matter how many times I run above program I get same result?
    Any idea why it is so

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It is very surpising and shoking to see that there is same garbage value at a position in main memory. No matter how many times I run above program I get same result?
    Any idea why it is so
    Because your program happens to read from the same memory location each time it is run? I do not think that this is the right approach especially since the contents of memory may not have very random properties to begin with.
    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

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Mario F's Signature
    Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin [John Von Neumann]
    Yup.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Jesus
    Let anyone among you who is without sin be the first to throw a stone at her.
    Some people call them 'pseudo-random numbers', but they are still in a state of sin either way
    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

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Code:
    Because your program happens to read from the same memory location each time it is run?
    But even after restarting my computer,I get same result. Is it such that garbage value is constant for a particular memory location.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Do yourself a big favor and don't try to read variables that don't store anything yet. (Okay an array could be called one variable, but don't pick on me; it's a collection of variables the same size given a single name.) It's dangerous.

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    But even after restarting my computer,I get same result. Is it such that garbage value is constant for a particular memory location.
    Your BIOS runs a RAM test every time you start your computer. The data may be left-over from the RAM test, or it may be left-over from something that Windows was doing during start-up.

    I'll bet if you did a statistical analysis of all the numbers in memory, some numbers would show-up more frequently than others. Small numbers would be more common than large numbers. Numbers that represent ASCII characters would be common. Numbers that represent CPU op-codes would be common, etc.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Hmm...,that seems to be quite logical.But can I check it

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Also, in debug mode, the compiler tends to insert code to fill memory with known values, just so that it can check a few things.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    Quote Originally Posted by Salem
    > One approach may be to use garbage value of variables
    1. even reading undefined values may cause exceptions
    2. you run off the end of the array

    Common implementations of rand() use this
    http://www.physics.orst.edu/~rubin/n...mc3/node2.html
    Such algorithms are only any good for student exercises.

    For statistical quality rand functions, consider this one
    http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

    For cryptographic quality rand functions, you really need an entropy pool based approach, like
    http://publib.boulder.ibm.com/infoce...les/random.htm
    Good suggestions, Salem!

    I would like to add (if the OP is open to more suggestions) that NETLIB has many random number generation routines. They happen to be written in FORTRAN, but they are pretty easy to translate into C++.

    Also, Boost has some random number libraries too:

    Boost Link


    Regards,


    David

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    a couple of years ago (when it released the p3) intel was talking about implementing a rand assembly instruction that took a truly random physical value (chip heat, electrical interference,etc) and returned it. Anyone ever hear what happened to that?

    BTW if you want a truly random value, get a geiger counter and some radioactive material and hook it up to your pc.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    www.random.org

    But please be polite, and don't use up all of them



    rand() uses a math algorithm, which doesn't provide true random numbers, but a sequence of numbers which at first glance APPEAR to be random, and pass statistical tests that random numbers pass.

    You could read up on the Mersenne Twister algorithm and try implementing it yourself. Good luck.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Cannot create shared memory
    By Phoenix_Rebirth in forum C Programming
    Replies: 3
    Last Post: 11-07-2008, 10:32 AM
  3. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 08:50 AM
  4. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 09:01 AM

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