Thread: Random character generator algorithm

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    Random character generator algorithm

    Will someone please critique my algorithm/plan?

    I need to write a program that will generate random characters (they can be either numbers, or upper or lower case letters) in the following format u328-iK23-o32k-A28j.

    My plan is to get a clock() reading after the user has entered a prompt, and use that to generate a random character. After the first and every subsequent random character is generated, I plan to use the clock at that point in the program to generate another one. Is this legitimate? Any comments, suggestions, or recommendations?

    Note: I need to do this without using any of the random number functions
    Last edited by m712; 12-07-2002 at 08:23 PM.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    How do you plan on using the clock to generate the random numbers? One thing to watch for is that if you generate all the numbers at roughly the same time each with a call to the clock, you'll get the same random numbers. Also, depending on what you do with the clock, you could end up with what looks like 1112-2233-3444-5556 or something similar.

    If you want to use the clock before each number, you need to make sure there is a lag between each clock call so they aren't called within the same millisecond or so.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Are you allowed to write your own random function?


    Code:
    unsigned int my_rand()
    {
        signed long param[]  = { std::time(NULL), 12345 };
        static signed long seed = (seed * param[1] + param[2]) % 2147483647;
        return static_cast<unsigned int>(seed / 3);
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    I am allowed to make my own functions, but I don't think I'm allowed to use the time function ( don't know what it is either, I'm only familiar with clock() ).

    I was planning to store the characters in an array somehow. For example have the clock reading correspond to an array element. I guess I should write code that manipulates the clock reading every time the code generates a character?
    Last edited by m712; 12-08-2002 at 12:11 AM.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Why don't you just use rand() to generate a mix of random numbers between the ASCII value of 'a' and the ASCII value of 'z',and random numbers between 0 and 9, and use
    srand(time(0)) to randomize?
    I think that's simple, isn't that what you want?
    none...

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    I gotta write this program without using rand() because it's an assignment for an intro. to computing class...we haven't disccused rand().

  7. #7
    Registered User -Xp-'s Avatar
    Join Date
    Nov 2002
    Posts
    28
    well tell then that you are way ahead of the rest and that you know rand(), and that its really not that hard.

    if you dont know rand(), are you capable of making your own radom number function? is this an impossiable assignment?
    No I DIDN'T steal my name from Misro$ofts OS, it's pure coincidence.

    The lines around my name (-) are only there because i needed a name over the 3 character minimum letter limit

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    If you want it to be simple, just take the current time and make some simple mathimatical operations, on it and you'll have a ramdom number...
    The use the % operator to have it limtted between two numbers.
    For example:
    (any number)%10 -> will be between 0 and 9
    5 + (any number)%10 -> will be betwenn 5 and 14

    Is that what you want?
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf returns random character
    By giannisapi in forum C Programming
    Replies: 3
    Last Post: 06-20-2009, 12:06 PM
  2. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Random character
    By rusty0412 in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2003, 06:29 AM