generating random variables

This is a discussion on generating random variables within the C++ Programming forums, part of the General Programming Boards category; Hi every body; I want to generate random variable ( N numbers), with display the time of generating of each ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    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
    Location
    Liverpool UK
    Posts
    1,409
    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."

    Stroustrup:
    "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
    Posts
    4
    i have used this code
    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++){
            t1[i]=rand()%250; 
    temps1[i]=clock(); 
         //    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well in a tight loop, all your clock() values are going to be the same.

    Perhaps you need some real gaps in time.
    Code:
        for(i=0; i<5; i++){
            t1[i]=rand()%250;
            temps1[i]=clock();
            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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Using srand() might help too.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    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
    Location
    New Zealand
    Posts
    6,295
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    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, 10: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, 05:55 PM

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