Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

how to generate a random number

Featured Replies

Posted

i wanna generate random numbers with no repeats, i have this code

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int randomNumber;
for(int i = 0; i < 15
; i++) //print random #s 15 times
{randomNumber = (rand() % 25) + 1;
cout << randomNumber << endl;getchar(
);
}
return 0;
}

but it generate the same sequence 17-18-10-1-20-25-4-9-13-15-6-21-7-3-12 everytime

how can i fix it

Try:

#include <cstdlib> 
#include <ctime>
#include <iostream>using namespace std;int main()
{
srand((unsigned)time(0));
int random_integer = rand();
cout << random_integer << endl;
}

In most coding languages the random number call will produce the same numbers everytime. We use the time as a seed value and generate the numbers from that

Edited by Kripton

Use time functions - like 'GetTickCount' - and mix results to get random numbers .

  • Author
Use time functions - like 'GetTickCount' - and mix results to get random numbers .

how i dont know this function could give a example

check this out: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Use time functions - like 'GetTickCount' - and mix results to get random numbers .

The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. It does not do random stuff.

how i dont know this function could give a example

using GetTickCount requires knowledge in win32 API programming. try google one of those.

Edited by Lumiliyab

The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. It does not do random stuff.

And this elapsed time is random by itself, you can get the same value from GetTickCount more than one time rarely .

  • Author
check this out: <a href="http://www.cplusplus.com/reference/clibrary/cstdlib/rand/" target="_blank">http://www.cplusplus.com/reference/clibrary/cstdlib/rand/</a>
Use time functions - like 'GetTickCount' - and mix results to get random numbers .

The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. It does not do random stuff.

how i dont know this function could give a example

using GetTickCount requires knowledge in win32 API programming. try google one of those.

thanks man i fix it!!!

Kripton answered your question already. Because you are using rand() you need to call srand() with a dynamic seed to allow rand() calls to actually be random.

Using timestamps, such as a unix timestamp, for your seed with other math involved is probably your best bet to have a unique seed as well as timestamps do not repeat in that manner.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.