Thread: generating random numbers using srand

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    generating random numbers using srand

    hi i want to generate numbers from 65 to 90 using srand and i dont want a number to be generated twice and put them in an array how can i do that

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ. Use an array. Check for duplicates. Use some loops.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read the FAQ
    There's an answer in there.
    http://faq.cprogramming.com/
    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.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    well i couldnt find i can generate numbers from 1 to 6 but when i try to generate 65 to 90 it generates 150 140 ... im a beginner

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Scroll down
    Read about GetRand

    > using srand
    srand() seeds the pseudo random sequence, use rand() to deliver each successive number in the pseudo random sequence.

    > i dont want a number to be generated twice
    Mmm, 65 to 90 - you mean 'A' to 'Z' ?
    char buff[26];
    for ( i = 0, c = 'A' ; i < 26 ; i++, c++ ) buff[ i ] = c;

    Or even
    char buff[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    That's the uniqueness bit sorted out.

    Now shuffle them to produce a random order.
    Do a board search, shuffling arrays was discussed quite recently
    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.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You don't generate the numbers using srand. srand seeds rand, giving the rand algorithm a starting value. rand() does the work. Check out this link.
    EDIT: Beaten.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  2. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. random numbers
    By ballmonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2005, 02:16 PM
  5. generating random numbers within a range
    By tucky in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 12:59 PM