Thread: Problem with random numbers.

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    14

    Problem with random numbers.

    Hi,
    i want to get random number between 1-100.
    i tried with rand(100) and the compiler dont recognize it.
    i added #include <stdlib.h>.
    please help.
    Or.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Try
    Code:
    #include "stdafx.h"
    #include "time.h"
    #include "stdlib.h"
    
    float random;
    srand(time(NULL));
    random=(rand()%101);

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    14

    Thanks, another one.

    Why not only random=rand()%100;
    working too.
    another question, maby you can help me with this.
    i have a struct like this:
    Code:
    struct job 
    	{
    		char Name[MAXNAME];
    		int Proirity;
             };
    how can i initialize the struct qucikly.
    i mean something like that : job arr[10];
    Code:
    arr[] = { {"name",3} {"name2",6} };
    thanks.
    Or.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You use the srand() call to seed the random number generator... it will always give you the same sequence if you don't.

    The array initialization will be compiler dependent. Compilers before C-99 may or may not accept parantetic initialization, but I believe most C-99 compliant ones do.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You can't assign directly to an array after you create it, but you can use an initializer (which means it has to be done at the point you create your array) as you have it, and it's valid C89 and C99:
    Code:
    struct job
    {
      char name[64];
      int pri;
    } arr[10] = {
      { "name1", 3 },
      { "name2", 10 },
    };
    To assign to the array later, after it's been initialized, you'll have to do something like:
    Code:
    strcpy(arr[2].name, "name3");
    arr[2].pri = 5;
    You could use other methods to “assign” to an array later on, such as:
    Code:
    const struct job otherjobs[sizeof arr] = { { "othername", 5 } };
    memcpy(arr, otherjobs, sizeof arr);
    This will replace all the contents of your old array with the new one (which only has one element set). But whether that's better or not... well, that's up to you to decide.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    14

    Thanks Alot!

    Thanks Alot!

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Pornflakes View Post
    Try
    Code:
    #include "stdafx.h"
    #include "time.h"
    #include "stdlib.h"
    
    float random;
    srand(time(NULL));
    random=(rand()%101);
    That's a very broken example!
    The correct calculation would be
    Code:
    random = rand() % 100 + 1;
    otherwise you get a number from 0 to 100 when he asked for a number from 1 to 100. 'random' should also be int, not float.
    You forgot to say that srand should only be called once for the whole program and not once per random number.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem creating unique random numbers
    By jamort in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2010, 03:05 AM
  2. Problem with random numbers
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2007, 10:30 AM
  3. Random numbers problem
    By ManiacBR in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2006, 12:34 PM
  4. problem with random numbers
    By xxwerdxx in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 08:56 AM
  5. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM