Google Answers Logo
View Question
 
Q: randomize ( No Answer,   1 Comment )
Question  
Subject: randomize
Category: Computers > Programming
Asked by: mott85-ga
List Price: $9.00
Posted: 14 May 2005 19:55 PDT
Expires: 15 May 2005 12:23 PDT
Question ID: 521744
I ahve theis pice of c code where i have the array equal a value i
want to chaneg those numbers to randon numbers. Part of the code can
be put soemwhere else if need be.
else if(simulation == 2){
     obsticles[0].x = 10;( i need this numebr to eb random)
obsticles[0].y = 40( i need this number to be random);
     obsticles[1].x = 11; obsticles[1].y = 40;
     obsticles[2].x = 12; obsticles[2].y = 40;
     obsticles[3].x = 13; obsticles[3].y = 40;
     obsticles[4].x = 14; obsticles[4].y = 40;
     obsticles[5].x = 15; obsticles[5].y = 40;
     obsticles[6].x = 16; obsticles[6].y = 40;
     obsticles[7].x = 17; obsticles[7].y = 40;
     obsticles[8].x = 18; obsticles[8].y = 40;
     obsticles[9].x = 19; obsticles[9].y = 40;
     x = 14; y = 34;
     targetx = 14;
     targety = 45;
   }
Answer  
There is no answer at this time.

Comments  
Subject: Re: randomize
From: pianoboy77-ga on 14 May 2005 22:15 PDT
 
See the example here:
http://www.cplusplus.com/ref/cstdlib/rand.html

As it shows there, you essentially need to:

1) Include the needed files at the top of your file:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

2) If you want different 'random' numbers each time you run your
program, initialize the random number generator based on the system
time like this:

srand ( time(NULL) );

3) get your random number by calling rand() % [the range] + [minimum value].
For example:
 
obsticles[0].x = rand()%100;  // x will be from 0 to 99 inclusive
obsticles[0].x = rand()%10 + 30; // x will be from 30 to 39 inclusive.

Hope this helps.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy