Results 1 to 10 of 10

Thread: [RESOLVED] Random number generation

  1. #1

    Thread Starter
    Addicted Member cyberwarpy's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    200

    Question [RESOLVED] Random number generation

    Hiya, Im just a novice in C++ ...

    What is the most efficient and effective method of generating a random number in C++ from a certain range of integers?
    Last edited by cyberwarpy; Aug 7th, 2002 at 08:12 AM.

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    	// random numbers from 1.. 10
    	for(int i=0;i<10;i++) printf("%d\n",rand()%10+1);
    	
    }

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Remember to call srand when your program starts, using something like srand(time(NULL)).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    what does "%d" in printf do?
    and can anyone briefly state the meaning of

    printf("%d\n",rand()%10+1);

    , I know it prints like

    1
    2
    3
    4
    5
    ...

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    %d ==> decimal (i.e. integer)

    rand() % 10 + 1 ==> Get a random number between 0 and 9, then add one to put it between 1 and 10.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I know your problem is resolved but I got better result using gettickcount than time.

    DWORD dwStart = GetTickCount();

    cout << dwStart % 10 +1<< endl;

    Just an other way to do it (you need to include windows.h).

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Don't use GetTickCount - it generates (semi-)predictable random numbers that aren't even random

    As a seed to any other generator it's ok though. The best way is to collect entropy from the system like mouse movements, time between keypresses, etc., stuff that will be pretty close to random. Under Linux (or recent ones, anyway) you can read from /dev/random to get a stream of random data
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    I heard they managed to get true random from quarks, not a pseudorandom number. Of course this technology isn't availible to the 'home user'!

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not from quarks, but is is a quantum effect. By putting quantum bits (e.g. a hydrogenium atom) in an undefined state, they will fall into one of the two defined states the next time you look at them - completly random and unpredictable. It works in quantum computers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    I didn't think it was quarks but I knew it had something to do with Quantum Physics.

Posting Permissions

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



Click Here to Expand Forum to Full Width