Batch random number through C executable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Batch random number through C executable

#1 Post by findstr » 20 Jul 2021 01:49

Hello!
I've read that batch %RANDOM% is not very random.
I thought it would be nice to have a .exe written in C programming language, which will return random number upon execution.
I've downloaded and installed Code::Blocks and tried different examples from the web, but all of them aren't producing anything good, they are using srand(time(NULL)); and rand();.
I've read that you need to create your own random number generator with example:
#define random(value) ((int)(((double)(rand())*(value))/(RAND_MAX+1.0)))
I can't figure how it works.

Can you, please, show me a C code which will generate random numbers?
Many thanks!

Squashman
Expert
Posts: 4293
Joined: 23 Dec 2011 13:59

Re: Batch random number through C executable

#2 Post by Squashman » 20 Jul 2021 09:53

findstr wrote:
20 Jul 2021 01:49
Can you, please, show me a C code which will generate random numbers?
Many thanks!
Well last I checked this wasn't a C programming forum.
Simple enough to call out to Powershell from a batch file and assign it to a variable.

Code: Select all

FOR /F "delims=" %%G IN ('powershell "GET-RANDOM"') do set _rand=%%G

Aacini
Expert
Posts: 1697
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Batch random number through C executable

#3 Post by Aacini » 20 Jul 2021 10:13

I suggest you to review this thread. There is an assembly source program to use RDRAND CPU instruction in order to generate random numbers and its ready-to-use .exe program.

Antonio

aGerman
Expert
Posts: 4198
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch random number through C executable

#4 Post by aGerman » 20 Jul 2021 11:47

While I agree with Squashman that this is not a C forum, just as an aside: Don't use the standard C functions to generate pseudorandom numbers. The generated values of rand() are as predictable as the values of the %random% variable. If you want to have a C-like functionality on Windows, rather use the BCryptGenRandom() API. There are plenty of examples how to use it.

Steffen

(FWIW: If you continue to fire and forget your threads, whithout giving feedback to the volunteers if you even read their responses, I'm going to stop answering your questions. Just saying.)

findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Re: Batch random number through C executable

#5 Post by findstr » 21 Jul 2021 01:04

Thanks, everyone!
The Powershell solution is what I need.
Assembly program looks interesting, I'll read about it.
aGerman wrote:
20 Jul 2021 11:47
(FWIW: If you continue to fire and forget your threads, whithout giving feedback to the volunteers if you even read their responses, I'm going to stop answering your questions. Just saying.)
Sorry about that. I just got used to others forums, where when they answer your question, and you reply "Thank you!", they complain about flooding the forums with useless posts.

Post Reply