Current time: 25 Apr 2024, 10:58 AM



Generating and Assigning Random ID
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#1
Generating and Assigning Random ID

I need to generate a random customer ID and assign it, if the ID already does not exist.

>So basically, for the moment I'm using array to store the IDs' (custid[10])
>Array size is 10 (for the moment)
>Generate a random number between 1 and 10
>Check if it already exists
>If it does not, assign to customer id array.


This is what I have tried;

Code:
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;

int custid[10]; //array to store assigned IDs'
int randomcid; //array to store generated random ID

int randomgen()
{
    srand(time(NULL));
    randomcid = rand() % 10 + 1;
    return 0;
}

int main()
{
    custid[0] = { 1, 5, 6 }; //filling the array with some values
    for (int a = 0; a <= 10; a++)
    {
        if (custid[a] == randomcid) //if the ID already exists
        {
            randomgen(); //generate another random number
        }
        else
        {
            custid[a] == randomcid;
        }
    }

    return 0;
}




What I want it to do is say it generated 2, and the array contains the values 1 6 and 3 in the first three indexes, I want it to skip the first three indexes and assign the number 2 to the fourth index.

Hope someone helps xD
[Image: SPOILER_FlasH-1.gif]
15 Apr 2016, 12:58 PM
Find Reply
Offline SmG Dragonrage

Royal Dragon
Senior Member
******
11 Years of Service
Posts: 4,018
Threads: 206
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Aug 2012
Reputation: 38
Location: Atlantis
Gavel of TrinityLeague of LegendsProgrammerCoolCrystal DonatorLegendary
#2
Re: Generating and Assigning Random ID

first off, dont return 0 from your randomgen function, and dont use global variables to store your random number. just return the randomcid from the randomgen function. then in your for loop, you are going to have to have an embedded for loop to check to see if the randomcid appears anywhere in the array. if it does you will have to generate a new one, otherwise you can use it. also when declaring your array, make all indexes that you dont want to use 0, that way you can tell if it has been assigned or not, as your randomgen will not produce a 0.
[Image: mla7mw8hs3u0g9czg.jpg?size_id=5]
Do not meddle in the affairs of Dragons, for you are crunchy and taste good with ketchup!
15 Apr 2016, 01:34 PM
Website Find Reply
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#3
Re: Generating and Assigning Random ID

SmG Dragonrage Wrote:first off, dont return 0 from your randomgen function, and dont use global variables to store your random number. just return the randomcid from the randomgen function. then in your for loop, you are going to have to have an embedded for loop to check to see if the randomcid appears anywhere in the array. if it does you will have to generate a new one, otherwise you can use it. also when declaring your array, make all indexes that you dont want to use 0, that way you can tell if it has been assigned or not, as your randomgen will not produce a 0.

Oh okay. I forgot to mention, I'm not creating all the IDs' at once. There is a function named Register Customer in which the customer is prompted to enter his details (name, address, etc) So when a new customer registers, it has to create a unique ID for that customer. (Only 1). So basically when a new customer registers it has to do this generating thing.
[Image: SPOILER_FlasH-1.gif]
15 Apr 2016, 11:11 PM
Find Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
SmG Gaming, © 2010-2024
Theme By: «SmG» Cloud
Edited by: «SmG» Wires