Hack Forums
[C] A Challenge for the bored 2 - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] A Challenge for the bored 2 (/showthread.php?tid=6068896)

Pages: Pages 1 2


A Challenge for the bored 2 - saltycracker - Apr 19, 2020

Write a function that will produce the numbers 1 - 10 randomly and not return the same value twice.

Example:

Say I call my function getNum

The first time I call getNum it may produce:
7

The second time I call getNum it may produce:
3

The third time I call getNum it nay produce:
10

The fourth time I call getNum it may produce:
8

The fifth time I call getNum it may produce:
2

The sixth time I call getNum it nay produce:
1

The seventh time I call getNum it nay produce:
4

The eigtht time I call getNum it may produce:
5

The nineth time I call getNum it may produce:
9

The tenth time I call getNum it nay produce:
6

Good luck... C++'ers, try to say away from container classes.

Note: What happens at the end of the cycle is up to you. You can reset the function to start again of just do nothing.


RE: A Challenge for the bored 2 - FoxBlood - Apr 19, 2020

Am I allowed to use rand() function?


RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020

(Apr 19, 2020 05:28 AM)FoxBlood Wrote:  Am I allowed to use rand() function?

Yes and srand too.

I just want people to think about a way to solve this without relying on a container class.


RE: A Challenge for the bored 2 - FoxBlood - Apr 19, 2020

Code
int arr[11];
int random_numer()
{
   int number = rand() % 10 + 1;
   if(arr[number] == 1)
       return random_numer();
   else
   {
       arr[number] = 1;
       return number;
   }
}
Something like this?


RE: A Challenge for the bored 2 - HFDone - Apr 19, 2020

very simple
with C you can use array for the rand values
with C++ you can use std::array


RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020

Very important... You must return the values 1 through 10 randomly and no repeats.


RE: A Challenge for the bored 2 - FoxBlood - Apr 19, 2020

Code
void random_numers()
{
    srand (time(NULL));
    int arr[11] = {0};
    for(int i = 1; i<=10;i++)
    {
        int number = rand() % 10 + 1;
        if(arr[number] == 1)
            i--;
        else
        {
            cout<<number<<" ";
            arr[number] = 1;
        }
    }
}
My best code I ever write. Isn't it?


RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020

(Apr 19, 2020 06:30 AM)FoxBlood Wrote:  
Code
void random_numers()
{
   srand (time(NULL));
   int arr[11] = {0};
   for(int i = 1; i<=10;i++)
   {
       int number = rand() % 10 + 1;
       if(arr[number] == 1)
           i--;
       else
       {
           cout<<number<<" ";
           arr[number] = 1;
       }
   }
}
My best code I ever write. Isn't it?
Wow! I mean Wow! Why would you write it like that? For lol's?


RE: A Challenge for the bored 2 - akali - Apr 19, 2020

Code
int getNum(int* array,int index){
    int r = 0;
    do{
        r = rand() % 11 ;
        for(int i = 0; i < index;++i){
            
            if(r == array[i]){
                r = 0;
            }
        }
    }while(r == 0);
    return r;
}
int main(){
    int array[10] = {0};
    for(int i = 0; i < 10; ++i){
        array[i] = getNum(array,i);
         printf("%i\n",array[i]);
    }
}



RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020

(Apr 19, 2020 06:55 AM)akali Wrote:  
Code
int getNum(int* array,int index){
   int r = 0;
   do{
       r = rand() % 11 ;
       for(int i = 0; i < index;++i){
           
           if(r == array[i]){
               r = 0;
           }
       }
   }while(r == 0);
   return r;
}
int main(){
   int array[10] = {0};
   for(int i = 0; i < 10; ++i){
       array[i] = getNum(array,i);
        printf("%i\n",array[i]);
   }
}

So far... You are making it way more difficult than it has to be. You don't need a loop to solve the problem.


RE: A Challenge for the bored 2 - HFDone - Apr 19, 2020

PHP Code

<?php 
int getNum
(int* array,int index){
   
int r = 0;
   
r = rand() % 10 + 1;
   for(
int i = 0; i < index; ++i){
      if(
r == array[i]){
         
r = getNum(array, i + 1);
      }
   }
   return 
r;
}

int main(){
   
int array[10] = {0};
   for(
int i = 0; i < 10; ++i){
       array[
i] = getNum(array, i);
        
printf("%i\n", array[i]);
   }
} 



RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020

(Apr 19, 2020 11:06 AM)HFDone Wrote:  
PHP Code

<?php 
int getNum
(int* array,int index){
 
  int r = 0;
 
  r = rand() % 10 + 1;
 
  for(int i = 0; i < index; ++i){
 
     if(r == array[i]){
 
        r = getNum(array, i + 1);
 
     }
 
  }
 
  return r;
}

int main(){
 
  int array[10] = {0};
 
  for(int i = 0; i < 10; ++i){
 
      array[i] = getNum(array, i);
 
       printf("%i\n", array[i]);
 
  }
} 

What is it with the loops? You don't need a loop or recursion to solve this problem.


RE: A Challenge for the bored 2 - severos - Apr 19, 2020

PHP Code

<?php 
int getNum
(){
  static 
int[] arr = {8,5,1,9,7,6,3,4,2,10}
  static 
idx = 0;
  if(
idx > 9)
    return 
0; // already done all numbers
  
return arr[idx++];
} 

Is this considered random enough?


RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020

Here's what I did..

Spoiler (Click to View)


RE: A Challenge for the bored 2 - 0xDEAD10CC - Apr 21, 2020

(Apr 19, 2020 10:06 PM)saltycracker Wrote:  Here's what I did..

Spoiler (Click to View)

Why define a constant ARR_SIZE then not use it to assign your size variable? That makes it nearly useless, you could have just omitted the constant and all usages of it to get the same effect. You also have an extra variable in there that you don't need:
[Image: gID61g4.png]