Thread: generating random variables

  1. #1
    Registered User
    Join Date
    Jan 2013

    generating random variables

    Hi every body;

    I want to generate random variable ( N numbers), with display the time of generating of each one.
    for exemple:
    the generated numbers the moment of generating it (second)
    12 0.000000
    200 0.000250
    158 0.000800
    365 0.00125

    thank you fo help

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    what have you done so far
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Jan 2013
    i have used this code
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> 
    int i=0;
    int t1[100];
    clock_t temps1[100]; 
    void main() {
    int i = 0;
        for(i=0; i<90; i++){
         //    printf("%d\t%f\n",t1[i],(double) temps1[i]/CLOCKS_PER_SEC);
    But i have a doubt in the used clock function

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well in a tight loop, all your clock() values are going to be the same.

    Perhaps you need some real gaps in time.
        for(i=0; i<5; i++){
            sleep( rand()%3+1 );
    Pick a small number of iterations to test with.
    A full 100 iterations could take up to 5 minutes with this code.

    Oh, and main returns int, not void.
    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.

  5. #5
    Join Date
    Dec 2007
    Using srand() might help too.

  6. #6
    Registered User
    Join Date
    Jan 2013
    thank you for replying
    the solution of using sleep function is the more approriate for me

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You'll get the same values every time unless you put a call to srand before the loop.
    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"

  8. #8
    Join Date
    Oct 2007
    Inside my computer
    Considering the printf, is this supposed to be C or C++?
    Also, you should consider fixing main and getting rid of global variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random number from set
    By Hybodus in forum C Programming
    Replies: 2
    Last Post: 12-20-2010, 11:54 AM
  2. generating random words
    By lilrayray in forum C Programming
    Replies: 24
    Last Post: 07-24-2006, 07:11 PM
  3. Generating Random Numbers
    By Flakster in forum C++ Programming
    Replies: 14
    Last Post: 08-22-2005, 12:50 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. Generating Random Numbers
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2002, 06:55 PM