Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, in this code I'm generate random number in specific range, but I want the out put of array must be
l101
l101
l102
l104

I mean adding the word "host" to every number that is generate randomly, how can do this please??
I'm sorry about my bad english

What I have tried:

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


 <pre>
    int main (int argc, char *argv[])
    {

      char buffer[16];
      srand(time(NULL));
      int n = rand() % (8 + 1 - 4) + 4 ;
      for(int i=0;i<n;i++){
         
         int r = rand() % (104 + 1 - 101) + 101 ;

          sprintf( buffer, "host%d", r );
          printf( "No. of random selected node = %s\n", buffer[i]);
      }
  }
Posted
Updated 19-Dec-17 5:40am
v4
Comments
jeron1 19-Dec-17 10:26am    
printf( "No. of random selected node = host%d\n", a[i]); // <== add the word 'host' here 
Lilyanaa 19-Dec-17 10:31am    
No, I want array must have "host101"...
jeron1 19-Dec-17 10:35am    
You want an array of strings?
Lilyanaa 19-Dec-17 10:41am    
yes, but how can fix this word "host" and make the number generate randomly??

Quote:
No, I want array must have "host101"...

Your array can't hold values like "host101" - it can only hold integers.
In order to get it to hold strings, you need to change it to an array that holds char pointers:
C#
char* a[50];
And then use malloc[^] to allocate space for each string inside your loop:
C#
char *str;
...
str = (char *) malloc(10);
You can then use sprintf[^] to print your "host" and the number into each string and then put it into your array.
 
Share this answer
 
Comments
Lilyanaa 19-Dec-17 11:15am    
Can you see my update please ?
OriginalGriff 19-Dec-17 11:26am    
And?
Assuming you have a random room number generated, you can make the string like this :
C++
char buffer[16];
sprintf( buffer, "host%d", roomNumber );

To generate the random room number I would use this :
C++
int GetRandomValue( int minval, int maxval )
{
   int span = maxval - minval;
   int rval = rand() % span;
   int result = rval + minval;
   return result;
}

If you want rooms to range from 101 to 110 then pass those two values to the function :
C++
int room = GetRandomValue( 101, 110 );
 
Share this answer
 
v2
Comments
Lilyanaa 19-Dec-17 11:17am    
Can you see my update please ?
Rick York 19-Dec-17 11:30am    
You are close. To display the buffer as a string you need to pass it to printf. As it is, you passed one character in the array to it - buffer[1]. It needs to be :

  printf( "No. of random selected node = %s\n", buffer );


If you want, you can generate a bunch of numbers and store them all in an array of integers and then you can print them all later using the same format and printf statement. That's up to you.
Lilyanaa 19-Dec-17 11:38am    
oh yes thanks a lot
Rick York 21-Dec-17 18:28pm    
Why did you unaccept the answer?
Lilyanaa 23-Dec-17 7:27am    
Oh ,I'm so sorry inadvertent error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900