Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

A Challenge for the bored 2

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.
May you be given directions by software engineer document writers.
Am I allowed to use rand() function?
(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.
May you be given directions by software engineer document writers.
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?
very simple
with C you can use array for the rand values
with C++ you can use std::array
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
Very important... You must return the values 1 through 10 randomly and no repeats.
May you be given directions by software engineer document writers.
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?
(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?
May you be given directions by software engineer document writers.
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]);
    }
}
(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.
May you be given directions by software engineer document writers.
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]);
   }
} 
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(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.
May you be given directions by software engineer document writers.
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?
Have a cool signature? hit me up and I might use it.
Here's what I did..

Spoiler (Click to View)
May you be given directions by software engineer document writers.
(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]
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by saltycrackerMay 29, 2020 04:36 AM 0 140 May 29, 2020 04:36 AM
Last Post: saltycracker
[C] by saltycrackerMay 23, 2020 06:48 PM 15 504 May 23, 2020 06:48 PM
Last Post: Litecoin
[C] by saltycrackerMay 1, 2020 01:21 AM 11 281 May 1, 2020 01:21 AM
Last Post: Dostoevsky